Maxwerty
Maxwerty

Reputation: 1

Trying to Update AttributedText for UIButton

I'm new here and in Swift. I am trying to update one of attributed text of the UIButton, specifically color of it. I found an option that seems can do it, but I have an error. Please help (see code below). If there are other options, please, let me know.

@IBAction func btnsPressed(_ sender: UIButton) {
    sender.updateTextAttributes(conversionHandler: convertToOptionalNSAttributedStringKeyDictionary([ NSAttributedString.Key.foregroundColor.rawValue : UIColor.orange]))
}

I have the following error: Cannot convert value of type [NSAttributedString.Key : Any]? to expected argument type ([NSAttributedString.Key : Any]) -> [NSAttributedString.Key : Any]

Upvotes: 0

Views: 221

Answers (1)

Reena Prajapati
Reena Prajapati

Reputation: 91

// .Selected
let selectedAttributedTitle = NSAttributedString(string: "Submit", 
                                                 attributes: [NSAttributedString.Key.foregroundColor : UIColor.green])
btnTap.setAttributedTitle(selectedAttributedTitle, for: .selected)

// .Normal
let normalAttributedTitle = NSAttributedString(string: "Submit",
                                               attributes: [NSAttributedString.Key.foregroundColor : UIColor.red])
btnTap.setAttributedTitle(normalAttributedTitle, for: .normal)

Try above code, its will be work.

Upvotes: 3

Related Questions