ios 根据状态以编程方式更改 UIButton 字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23508618/
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
Programmatically changing UIButton font color depending on state
提问by Ian
I'm trying to programmatically create a UIButton. However by default it's turning up white (which is the default color for my navigation bar, I feel that's relevant). I want it to just be Apple's default blue color in iOS7. I've managed to change the color for it's default state, but the moment I select it the text becomes white again. I cannot figure out how to keep it blue.
我正在尝试以编程方式创建一个 UIButton。但是默认情况下它会变成白色(这是我导航栏的默认颜色,我觉得这是相关的)。我希望它只是苹果在 iOS7 中的默认蓝色。我设法更改了它的默认状态的颜色,但是当我选择它时,文本再次变为白色。我不知道如何让它保持蓝色。
Could someone please explain to me how I can programmatically create a UIButton and have it act the same as though I created it in storyboard?
有人可以向我解释我如何以编程方式创建 UIButton 并让它的行为与我在故事板中创建的一样吗?
Current code:
当前代码:
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(320 - 150, 0, 140, 40);
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
cancelButton.titleLabel.tintColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
cancelButton.titleLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
Thank you.
谢谢你。
回答by Mind Pixel
Try this code :
试试这个代码:
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:CGRectMake(320 - 150, 0, 140, 40)];
[cancelButton setBackgroundColor:[UIColor clearColor]];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; // This will helps you during click time title color will be blue color
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(button_Action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelButton];
回答by iPatel
Just tried with
刚试过
[cancelButton setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0/255.0 alpha:1.0] forState:UIControlStateNormal];
For more information read official documentation of UIButton
.
欲了解更多信息,请阅读的官方文件UIButton
。
回答by Apple
This will solve your problem,
这将解决您的问题,
[cancelButton setTitleColor:YOUR COLOR forState:SPECIFIED STATE];
The state values are :
状态值为:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
回答by Sunny Shah
Have you tried this
你有没有试过这个
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
回答by Dave Anderson
Swift:
迅速:
let button = UIButton (type: UIButtonType.system)
This will create a button that uses the standard tint color just like a button added to a storyboard in interface builder.
这将创建一个使用标准色调颜色的按钮,就像添加到界面构建器中故事板的按钮一样。
回答by Aloha Silver
Try using
尝试使用
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
for specifying colors for all the required states.
用于指定所有必需状态的颜色。