【问题标题】:How to change attributed stringkey foreground color in button A from button B?如何从按钮 B 更改按钮 A 中的属性字符串键前景色?
【发布时间】: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


    【解决方案1】:

    这样做,

    if let btnBTitle = buttonB.title(for: .normal) {
         let attributedTextFromButtonB = NSMutableAttributedString(string: btnBTitle, attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])  
         buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 2018-01-06
      • 2017-03-24
      • 2016-12-11
      • 2022-01-16
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多