uisegmentedcontrol - 为 UISegmentedControl 设置彩色文本范围
问题描述
根据以下文档(https://developer.apple.com/documentation/uikit/uisegmentedcontrol/1618570-settitletextattributes)
我应该能够添加属性来更改它在特定模式下的外观。
modalitySegmentedControl.setTitle("LDR ("+(stateController?.tdfvariables.selectedRadionuclide.name ?? "-") + ")", forSegmentAt: Constants.LDRButton)
let colorAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.systemTeal ]
modalitySegmentedControl.setTitleTextAttributes(colorAttribute, for: .selected)
总之文字上的控件基本上是"LDR (I-125)"
。目前,此代码突出显示整个选择蓝绿色。我正在寻找一种仅用(I-125)
青色突出显示唯一的方法。我可以通过定义属性作用的范围来使用常规 UILabel 来做到这一点,但我似乎找不到使用 UISegmentedControl 设置特定颜色范围的方法?
这可能吗?
它目前看起来像这样:
我希望它LDR
是白色的,并且只在(I-125)部分是青色的。
解决方案
简而言之,我认为这是不可能的。检查我的hacky游乐场:
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
extension UIView {
class func getAllSubviews<T: UIView>(from parentView: UIView) -> [T] {
return parentView.subviews.flatMap { subView -> [T] in
var result = getAllSubviews(from: subView) as [T]
if let view = subView as? T { result.append(view) }
return result
}
}
class func getAllSubviews(from parentView: UIView, types: [UIView.Type]) -> [UIView] {
return parentView.subviews.flatMap { subView -> [UIView] in
var result = getAllSubviews(from: subView) as [UIView]
for type in types {
if subView.classForCoder == type {
result.append(subView)
return result
}
}
return result
}
}
func getAllSubviews<T: UIView>() -> [T] { return UIView.getAllSubviews(from: self) as [T] }
func get<T: UIView>(all type: T.Type) -> [T] { return UIView.getAllSubviews(from: self) as [T] }
func get(all types: [UIView.Type]) -> [UIView] { return UIView.getAllSubviews(from: self, types: types) }
}
class MyViewController : UIViewController {
var myString: String = "LDR (I-125)"
var myString42: String = "424242424242"
var attributedString = NSMutableAttributedString()
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let items = ["EBRT", "LDR (I-125)", "PERM"]
let modalitySegmentedControl = UISegmentedControl(items: items)
modalitySegmentedControl.frame = CGRect(x: 20, y: 200, width: 300, height: 20)
modalitySegmentedControl.backgroundColor = .white
attributedString = NSMutableAttributedString(string: myString, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18)])
attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:4, length:7))
let subviews = modalitySegmentedControl.getAllSubviews()
for view in subviews {
if view is UILabel {
if let label = view as? UILabel, label.text == myString {
print(label.attributedText)
label.attributedText = attributedString
//label.text = "42" // this works
print(label.attributedText) // looks changed
}
}
}
let subviews2 = modalitySegmentedControl.getAllSubviews()
for view in subviews2 {
if view is UILabel {
if let label = view as? UILabel, label.text == myString {
print(label.attributedText) // but it didn't change
}
}
}
let lab = UILabel()
lab.frame = CGRect(x: 40, y: 250, width: 300, height: 20)
lab.attributedText = attributedString
view.addSubview(lab)
view.addSubview(modalitySegmentedControl)
self.view = view
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
推荐阅读
- docker - 容器化单体的可扩展性是否不如微服务?
- javascript - 如何使用 VueJS 实现 Gmail API
- snowflake-cloud-data-platform - Snowflake:以多种分隔符格式导出数据
- ruby - Ruby 版本失败 2.7.0:错误:Permission denied @ apply2files - /usr/local/share/Library/
- amazon-ec2 - 断开和重新连接 nvme
- json - 从 SwiftUI 中的 URL 获取 JSON
- python - 多个 groupby 并获得唯一计数
- docker - python Mysql.connector 不连接但 mysql 服务器已启动并正在工作
- javascript - CSS 中的悬停和 JavaScript 中的可见性是否不兼容?
- go - 向项目添加 gomock 测试会在运行测试时提供“构建约束排除所有 Go 文件”