gpt4 book ai didi

uisegmentedcontrol - 为 UISegmentedControl 设置彩色文本范围

转载 作者:行者123 更新时间:2023-12-04 04:10:27 35 4
gpt4 key购买 nike

根据以下文档 ( 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) 的方法只有蓝绿色。我可以通过定义属性所作用的 range 来使用常规 UILabel 来做到这一点,但我似乎无法找到一种方法来使用 UISegmentedControl 设置特定的颜色范围?

这可能吗?

目前看起来是这样的:

enter image description here

我想要 LDR白色并且在(I-125)部分只有青色

最佳答案

总之我觉得不可能。检查我的 hacky playground:

//: 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()

您可以找到 UISegmentedControl 的特定 UILabel subview ,甚至可以更改文本,但属性更改不起作用。 changing attributedText changing text

Related question: Segmented Control set attributed title in each segment

关于uisegmentedcontrol - 为 UISegmentedControl 设置彩色文本范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61831978/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com