ios 选择时更改按钮文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6077760/
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:02:23  来源:igfitidea点击:

Changing the button text color when selected

iphoneobjective-ccocoa-touchiosuibutton

提问by Abhinav

I have a custom button and I am setting the different image when it is highlighted. Now, I want to change the color of the text on the button when button is highlighted. Is it possible to do this?

我有一个自定义按钮,当它突出显示时,我正在设置不同的图像。现在,我想在突出显示按钮时更改按钮上文本的颜色。是否有可能做到这一点?

回答by Nick Weaver

You can do it this way:

你可以这样做:

[myButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

回答by King-Wizard

Swiftversion:

迅捷版:

myButton.setTitleColor(UIColor.greenColor(), forState: UIControlState.Highlighted)

回答by pasql

You could also achieve this with storyboards by selecting in the attributes inspector under Button the State Config to Selected or Highlighted and style the button right there - no code needed :)

您还可以通过在 Button 下的属性检查器中选择 State Config to Selected 或 Highlighted 并在那里设置按钮样式来使用故事板来实现这一点 - 不需要代码:)

回答by Vinicius Carvalho

[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

or

或者

[myButton setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];

This works for me.

这对我有用。