Как вы устанавливаете атрибутный цвет заголовка для состояния в Swift
Я могу установить attributedTitle
для UIControlState
, но как бы установить атрибутный цвет заголовка для UIControlState
?
Ответы
Ответ 1
// 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)
Ответ 2
Swift 4
// 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)
Ответ 3
Swift 4
@IBOutlet weak var btnforgetPassword:UIButton!{
didSet{
btnforgetPassword.setAttributedTitle(NSAttributedString(string: "Forget Password?", attributes: [NSAttributedStringKey.foregroundColor :UIColor.white ] ), for: .normal
}
}