ios UIButton 标题文字颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6703699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton title text color
提问by Ali
I'm setting text color for UIButton
我正在设置文本颜色 UIButton
headingButton.titleLabel.textColor = [UIColor colorWithRed:36/255.0
green:71/255.0
blue:113/255.0
alpha:1.0];
It's not changing color same code I'm using in another code it's working.
它不会改变我在另一个正在工作的代码中使用的相同代码的颜色。
回答by Amit Singh
use
用
Objective-C
目标-C
[headingButton setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];
Swift
迅速
headingButton.setTitleColor(.black, for: .normal)
回答by Alex Cio
I created a custom class MyButtonextended from UIButton
. Then added this inside the Identity Inspector
:
我创建了一个自定义类myButton的距离延长UIButton
。然后在里面添加了这个Identity Inspector
:
After this, change the button type to Custom:
在此之后,将按钮类型更改为Custom:
Then you can set attributes like textColor
and UIFont
for your UIButton
for the different states:
然后,您可以为不同的状态设置类似textColor
和UIFont
适合您的属性UIButton
:
Then I also created two methods inside MyButton
class which I have to call inside my code when I want a UIButton
to be displayed as highlighted:
然后我还在MyButton
类中创建了两个方法,当我希望 aUIButton
显示为突出显示时,我必须在我的代码中调用它们:
- (void)changeColorAsUnselection{
[self setTitleColor:[UIColor colorFromHexString:acColorGreyDark]
forState:UIControlStateNormal &
UIControlStateSelected &
UIControlStateHighlighted];
}
- (void)changeColorAsSelection{
[self setTitleColor:[UIColor colorFromHexString:acColorYellow]
forState:UIControlStateNormal &
UIControlStateHighlighted &
UIControlStateSelected];
}
You have to set the titleColor
for normal, highlight and selected UIControlState
because there can be more than one state at a time according to the documentation of UIControlState
.
If you don't create these methods, the UIButton
will display selection or highlighting but they won't stay in the UIColor
you setup inside the UIInterface Builder
because they are just available for a short display of a selection, not for displaying selection itself.
您必须设置titleColor
for normal、highlight 和 selected,UIControlState
因为根据UIControlState
. 如果您不创建这些方法,它们UIButton
将显示选择或突出显示,但它们不会保留在UIColor
您设置的内部,UIInterface Builder
因为它们仅可用于简短显示选择,而不用于显示选择本身。
回答by Prashant chaudhary
In Swift:
在斯威夫特:
Changing the label text color is quite different than changing it for a UIButton
. To change the text color for a UIButton
use this method:
更改标签文本颜色与更改UIButton
. 要更改UIButton
使用此方法的文本颜色:
self.headingButton.setTitleColor(UIColor(red: 107.0/255.0, green: 199.0/255.0, blue: 217.0/255.0), forState: UIControlState.Normal)
回答by NickCoder
swift 5 version:
快速 5 版本:
By using default inbuilt color:
通过使用默认的内置颜色:
button.setTitleColor(UIColor.green, for: .normal)
button.setTitleColor(UIColor.green, for: .normal)
OR
或者
You can use your custom color by using RGB method:
您可以使用 RGB 方法使用自定义颜色:
button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)
button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)