如何在Swift中为State设置归属标题颜色

时间:2014-12-23 17:06:22

标签: swift uibutton

我可以为attributedTitle设置UIControlState,但如何为UIControlState设置属性标题的颜色?

3 个答案:

答案 0 :(得分:45)

// 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)

答案 1 :(得分:11)

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)

答案 2 :(得分:0)

Swift 5功能设置颜色和字体。

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)
    }
}