如何从按钮B更改按钮A中的属性stringkey前景色?

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

我有按钮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)

谢谢!

ios swift nsattributedstring nsmutableattributedstring
1个回答
0
投票

这样做,

if let btnBTitle = buttonB.title(for: .normal) {
     let attributedTextFromButtonB = NSMutableAttributedString(string: btnBTitle, attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])  
     buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)
}
© www.soinside.com 2019 - 2024. All rights reserved.