【问题标题】:How do you set an Attributed Title Color for State in Swift如何在 Swift 中为状态设置属性标题颜色
【发布时间】:2014-12-23 17:06:21
【问题描述】:

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

【问题讨论】:

  • 在您的标题属性字典中:NSForegroundColorAttributeName: someColor
  • 对,工作正常,但这不允许我根据 .Selected 控件状态设置颜色
  • 使用 setAttributedText 控制状态 .... 这应该可以...

标签: swift uibutton


【解决方案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)

【讨论】:

  • @NatashaTheRobot 你能帮我在选定的情况下在这里工作吗?
【解决方案2】:

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)

斯威夫特 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 我已经使用了这段代码,但它在选定的情况下不起作用。你能解释一下整个步骤吗?
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2016-07-18
    • 2014-12-23
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多