我有按钮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中的字符串是动态的,所以我没有用于创建另一个attributionText的确切字符串并从按钮B执行此操作。
let attributedTextFromButtonB = NSMutableAttributedString(string: "127", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
bt.setAttributedTitle(attributedText, for: .normal)
buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)
谢谢!
这样做,
if let btnBTitle = buttonB.title(for: .normal) {
let attributedTextFromButtonB = NSMutableAttributedString(string: btnBTitle, attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)
}