Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set an Attributed Title Color for State in Swift

Tags:

swift

uibutton

I'm able to set the attributedTitle for a UIControlState, but how would I set the attributed title's color for a UIControlState?

like image 443
landing Avatar asked Dec 23 '14 17:12

landing


People also ask

How do you make an attributed string in Swift?

Creating an Attributed String from MarkdownUse Markdown syntax to initialize an attributed string with text and attributes. Use a Markdown-syntax string to iniitalize an attributed string with standard or custom attributes.

What is NSAttributedString?

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.


2 Answers

// create the button let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) button.backgroundColor = UIColor.yellowColor()  // set the attributed title for different states  // .Selected let mySelectedAttributedTitle = NSAttributedString(string: "Click Here",     attributes: [NSForegroundColorAttributeName : UIColor.greenColor()]) button.setAttributedTitle(mySelectedAttributedTitle, forState: .Selected)  // .Normal let myNormalAttributedTitle = NSAttributedString(string: "Click Here",     attributes: [NSForegroundColorAttributeName : UIColor.blueColor()]) button.setAttributedTitle(myNormalAttributedTitle, forState: .Normal) 
like image 69
NatashaTheRobot Avatar answered Sep 27 '22 23:09

NatashaTheRobot


Swift 4 & 5

    // create the button     let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))     button.backgroundColor = UIColor.yellow          // set the attributed title for different states          // .Selected     let mySelectedAttributedTitle = NSAttributedString(string: "Click Here",                                                        attributes: [NSAttributedStringKey.foregroundColor : UIColor.green])     button.setAttributedTitle(mySelectedAttributedTitle, for: .selected)          // .Normal     let myNormalAttributedTitle = NSAttributedString(string: "Click Here",                                                      attributes: [NSAttributedStringKey.foregroundColor : UIColor.blue])               button.setAttributedTitle(myNormalAttributedTitle, for: .normal) 

Swift 3

// create the button let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) button.backgroundColor = UIColor.yellow  // set the attributed title for different states  // .Selected let mySelectedAttributedTitle = NSAttributedString(string: "Click Here",                                                attributes: [NSForegroundColorAttributeName : UIColor.green]) button.setAttributedTitle(mySelectedAttributedTitle, for: .selected)  // .Normal let myNormalAttributedTitle = NSAttributedString(string: "Click Here",                                              attributes: [NSForegroundColorAttributeName : UIColor.blue])   button.setAttributedTitle(myNormalAttributedTitle, for: .normal) 
like image 40
kuzdu Avatar answered Sep 28 '22 00:09

kuzdu