【发布时间】:2018-05-24 10:58:38
【问题描述】:
我有带有自定义属性文本的按钮 A。有没有办法用按钮 B 改变它的前景色?
这是按钮 A:
let buttonA: UIButton = {
let bt = UIButton(type: .system)
bt.titleLabel?.numberOfLines = 0
let attributedText = NSMutableAttributedString(string: "127", attributes: [NSAttributedStringKey.foregroundColor : UIColor.black])
bt.setAttributedTitle(attributedText, for: .normal)
return bt
}()
我只想更改颜色并记住 NSMutableAttributedString 中的字符串是动态的,因此我没有确切的字符串可用于创建另一个属性文本并从按钮 B 执行此操作。
let attributedTextFromButtonB = NSMutableAttributedString(string: "127", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
bt.setAttributedTitle(attributedText, for: .normal)
buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)
谢谢!
【问题讨论】:
标签: ios swift nsattributedstring nsmutableattributedstring