我正在为 UIButton
设置文本颜色
headingButton.titleLabel.textColor = [UIColor colorWithRed:36/255.0
green:71/255.0
blue:113/255.0
alpha:1.0];
它不会改变我在另一个正在工作的代码中使用的相同代码的颜色。
利用
Objective-C
[headingButton setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];
迅速
headingButton.setTitleColor(.black, for: .normal)
我创建了一个从 UIButton
扩展的自定义类 MyButton。然后在 Identity Inspector
中添加:
https://i.stack.imgur.com/SWmMI.png
在此之后,将按钮类型更改为自定义:
https://i.stack.imgur.com/xkPq2.png
然后,您可以为您的 UIButton
设置不同状态的 textColor
和 UIFont
等属性:
https://i.stack.imgur.com/t1Xhj.png
然后我还在 MyButton
类中创建了两个方法,当我希望将 UIButton
显示为突出显示时,我必须在代码中调用它们:
- (void)changeColorAsUnselection{
[self setTitleColor:[UIColor colorFromHexString:acColorGreyDark]
forState:UIControlStateNormal &
UIControlStateSelected &
UIControlStateHighlighted];
}
- (void)changeColorAsSelection{
[self setTitleColor:[UIColor colorFromHexString:acColorYellow]
forState:UIControlStateNormal &
UIControlStateHighlighted &
UIControlStateSelected];
}
您必须将 titleColor
设置为正常、突出显示和选中 UIControlState
,因为根据 UIControlState
的文档,一次可以有多个状态。如果您不创建这些方法,UIButton
将显示选择或突出显示,但它们不会保留在您在 UIInterface Builder
中设置的 UIColor
中,因为它们仅可用于短暂显示选择,而不是用于显示选择本身。
|
而不是 &
吗?
在斯威夫特:
更改标签文本颜色与更改 UIButton
完全不同。要更改 UIButton
的文本颜色,请使用以下方法:
self.headingButton.setTitleColor(UIColor(red: 107.0/255.0, green: 199.0/255.0, blue: 217.0/255.0), forState: UIControlState.Normal)
迅捷5版本:
通过使用默认的内置颜色:
button.setTitleColor(UIColor.green, for: .normal)
或者
您可以使用 RGB 方法使用自定义颜色:
button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)
除了 de color,我的问题是我正在使用 textlabel 设置文本
bt.titleLabel?.text = title
我解决了更改为:
bt.setTitle(title, for: .normal)
不定期副业成功案例分享