为 UISegmentedControl 设置彩色文本范围

Set range of colored text for 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) 的方法。我可以通过定义属性所作用的 range 来使用常规 UILabels 来做到这一点,但我似乎无法找到一种方法来使用 UISegmentedControl?[=16 设置特定的颜色范围=]

这可以吗?

目前看起来是这样的:

我希望 LDR 白色 并且在 [=30 上只有 青色 =](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 子视图,甚至可以更改文本,但属性更改不起作用。

Related question: Segmented Control set attributed title in each segment