简体   繁体   English

如何以编程方式更改UIButton属性文本的颜色?

[英]How to change UIButton attributed text colour programatically?

I have a subclass (KeyButton) of UIButton where I am applying certain styles for the button. 我有UIButton的子类(KeyButton),在其中为按钮应用某些样式。 The following code adds the attributed text for buttons in the ViewController. 以下代码在ViewController中添加按钮的属性文本。

func superScriptText(text: String, button: KeyButton, fontSize: Int) {
    let font:UIFont? = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.light)
    let fontSuper:UIFont? = UIFont(name: "Helvetica", size:CGFloat(fontSize))
    let attString:NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: [.font:font!])
    attString.setAttributes([.font:fontSuper!,.baselineOffset:15], range: NSRange(location:1,length:1))
    button.setAttributedTitle(attString, for: .normal)
}

How can I change the color of attributed text for the button in the class? 如何更改类中按钮的属性文本的颜色?

Just change: 只是改变:

let attString:NSMutableAttributedString = 
     NSMutableAttributedString(string: text, attributes: [.font:font!])

to: 至:

let attString:NSMutableAttributedString = 
     NSMutableAttributedString(string: text, attributes: [.font:font!, .foregroundColor: UIColor.red])

NSAttributedStringKey.foregroundColor is used for the text color, see more options in docs . NSAttributedStringKey.foregroundColor用于文本颜色,请参阅docs中的更多选项。

You have to add the .foregroundColor key with a UIColor object as the value to the NSAttributedString s attributes dictionary. 您必须将带有UIColor对象的.foregroundColor键添加为NSAttributedStringattributes字典的值。

Example (assuming you have added a custom button in storyboard): 示例(假设您已在情节提要中添加了自定义按钮):

class CustomButton: UIButton {
    override func awakeFromNib() {
        super.awakeFromNib()

        let text = "CustomButton"

        let font = UIFont.systemFont(ofSize: UIFont.buttonFontSize)
        let textColor = UIColor.orange

        let attributes: [NSAttributedStringKey: Any] = [
            .font: font,
            .foregroundColor: textColor
        ]

        let attributedText = NSAttributedString(string: text, attributes: attributes)
        self.setAttributedTitle(attributedText, for: .normal)
    }
}

I couldn't do this through UIButton subclass. 我无法通过UIButton子类执行此操作。 I created the subclass of NSAttributtedText and add the following method: 我创建了NSAttributtedText的子类,并添加了以下方法:

var textColor: UIColor?

func setSuperScript(text: String, button: KeyButton, fontSize: Int) {
    let font:UIFont? = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.light)
    let fontSuper:UIFont? = UIFont(name: "Helvetica", size:CGFloat(fontSize))
    let attString:NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: [.font:font!, .foregroundColor: textColor!])
    attString.setAttributes([.font:fontSuper!,.baselineOffset:15, .foregroundColor: textColor!,], range: NSRange(location:1,length:1))
    button.setAttributedTitle(attString, for: .normal)
}

I am setting the color based on the logic I have and then set the attributed string color accordingly. 我根据已有的逻辑设置颜色,然后相应地设置属性字符串的颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM