I'm able to set the attributedTitle
for a UIControlState
, but how would I set the attributed title's color for a UIControlState
?
Asked
Active
Viewed 5.1k times
32

DeyaEldeen
- 10,847
- 10
- 42
- 75

landing
- 449
- 1
- 4
- 8
-
In your title attributes dictionary: `NSForegroundColorAttributeName: someColor` – Ian Dec 23 '14 at 17:09
-
1Right, that works fine, but that doesn't let me set the color based on the .Selected control state – landing Dec 23 '14 at 17:32
-
use the setAttributedText for control state .... this shall do... – JRafaelM May 29 '18 at 20:47
3 Answers
68
// 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)

NatashaTheRobot
- 6,879
- 4
- 32
- 27
-
1@NatashaTheRobot can you please help me its not working in selected case here? – Sandip Gill Jan 18 '18 at 10:34
21
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)

kuzdu
- 7,124
- 1
- 51
- 69
-
3Hi @kuzdu I have used this code but its not working in selected case. can you please explain whole step ? – Sandip Gill Jan 18 '18 at 10:03
-
1
Swift 5 functions to set the color and font.
class func setButtonTextColor( _ button:UIButton , color:UIColor ) {
if let str = button.titleLabel?.attributedText {
let attributedString = NSMutableAttributedString( attributedString: str )
attributedString.removeAttribute(.foregroundColor, range: NSRange.init(location: 0, length: attributedString.length))
attributedString.addAttributes(
[NSAttributedString.Key.foregroundColor : color],
range: NSRange.init(location: 0, length: attributedString.length)
)
button.setAttributedTitle(attributedString, for: .normal)
}
}
class func setButtonTextFont( _ button:UIButton, font: UIFont) {
if let str = button.titleLabel?.attributedText {
let attributedString = NSMutableAttributedString( attributedString: str )
attributedString.removeAttribute(.font, range: NSRange.init(location: 0, length: attributedString.length))
attributedString.addAttributes(
[NSAttributedString.Key.font : font ],
range: NSRange.init(location: 0, length: attributedString.length)
)
button.setAttributedTitle(attributedString, for: .normal)
}
}

Wil
- 351
- 3
- 9