【问题标题】:How to change the title text color of UI Button?如何更改 UI Button 的标题文本颜色?
【发布时间】:2021-07-21 00:12:44
【问题描述】:

我尝试更改按钮文本的颜色,但它仍然保持白色。请帮帮我。

isbeauty = UIButton()
isbeauty.setTitle("Buy", forState: UIControlState.Normal)
isbeauty.titleLabel?.textColor = UIColorFromRGB("F21B3F")
isbeauty.titleLabel!.font = UIFont(name: "AppleSDGothicNeo-Thin" , size: 25)
isbeauty.backgroundColor = UIColor.clearColor()
isbeauty.layer.cornerRadius = 5
isbeauty.layer.borderWidth = 1
isbeauty.layer.borderColor = UIColorFromRGB("F21B3F").CGColor
isbeauty.frame = CGRectMake(300, 134, 55, 26)
isbeauty.addTarget(self,action: "first:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(isbeauty)

【问题讨论】:

  • 避免直接访问titleLabel 可以使用与forState: 等效的方法。而是使用setTitleColor(_:forState:)

标签: ios swift uibutton


【解决方案1】:

例如 - 白色标题文本,突出显示时为灰色(点击向下):

isbeauty.setTitleColor(.white, for: .normal)
isbeauty.setTitleColor(.gray, for: .highlighted)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2021-06-17
    • 2017-07-11
    相关资源
    最近更新 更多