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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:47:14  来源:igfitidea点击:

UIButton title text color

iosobjective-ciphoneuibuttontextcolor

提问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

enter image description here

在此处输入图片说明

After this, change the button type to Custom:

在此之后,将按钮类型更改为Custom

enter image description here

在此处输入图片说明

Then you can set attributes like textColorand UIFontfor your UIButtonfor the different states:

然后,您可以为不同的状态设置类似textColorUIFont适合您的属性UIButton

enter image description here

在此处输入图片说明

Then I also created two methods inside MyButtonclass which I have to call inside my code when I want a UIButtonto 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 titleColorfor normal, highlight and selected UIControlStatebecause there can be more than one state at a time according to the documentation of UIControlState. If you don't create these methods, the UIButtonwill display selection or highlighting but they won't stay in the UIColoryou setup inside the UIInterface Builderbecause they are just available for a short display of a selection, not for displaying selection itself.

您必须设置titleColorfor 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 UIButtonuse 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:

通过使用默认的内置颜色:

  1. button.setTitleColor(UIColor.green, for: .normal)
  1. button.setTitleColor(UIColor.green, for: .normal)

OR

或者

You can use your custom color by using RGB method:

您可以使用 RGB 方法使用自定义颜色:

  1. button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)
  1. button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)