landing landing -4 years ago 193
Swift Question

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

I'm able to set the

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

Answer Source
// 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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download