-1

I have button A with customized attributed text. Is there a way that I can change it foreground color with button B?

This is button 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
}()

I would like to change only color and keep in mind string in NSMutableAttributedString is dynamic so I don't have the exact string to use to create another attributedText and do this from button B.

let attributedTextFromButtonB = NSMutableAttributedString(string: "127", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
bt.setAttributedTitle(attributedText, for: .normal)  
buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)

Thanks!

KevinVuD
  • 581
  • 2
  • 5
  • 25

1 Answers1

0

Do Like this,

if let btnBTitle = buttonB.title(for: .normal) {
     let attributedTextFromButtonB = NSMutableAttributedString(string: btnBTitle, attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])  
     buttonA.setAttributedTitle(attributedTextFromButtonB, for: .normal)
}
Venk
  • 5,949
  • 9
  • 41
  • 52