设置UISegmentedControl的彩色文本范围。

问题描述 投票:0回答:1

根据以下文件(https:/developer.apple.comdocumentationuikituisegmentedcontrol1618570-setitletextattributes。)

我应该可以添加属性来改变它在特定模式下的样子。

            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) 只用茶色。 我可以在常规的UIL标签中通过定义一个 范围 但我似乎找不到用UISegmentedControl设置特定颜色范围的方法。

可以这样做吗?

目前的情况是这样的。

enter image description here

我想让 LDR 将要 白颜色 而且只 在...上 (I-125) 部分。

uisegmentedcontrol swift5 nsrange nsmutableattributedstring
1个回答
1
投票

总之,我认为这是不可能的。检查我的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()

你可以找到具体的 UILabel 子视图 UISegmentedControl 甚至可以修改文字,但属性修改却不行。changing attributedTextchanging text

相关问题 分段控制在每段中设置属性标题。

© www.soinside.com 2019 - 2024. All rights reserved.