选择时更改按钮文本颜色

时间:2022-11-04 21:45:08

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?

我有一个自定义按钮,我正在设置不同的图像时突出显示。现在,我想要在高亮显示按钮时更改按钮上文本的颜色。有可能这样做吗?

4 个解决方案

#1


43  

You can do it this way:

你可以这样做:

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

#2


3  

Swift version:

斯威夫特版本:

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

#3


1  

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 :)

您还可以通过在按钮下的属性检查器中选择状态配置到选中或突出显示,并在那里对按钮进行样式化——不需要代码:)

#4


0  

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

or

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

This works for me.

这适合我。

#1


43  

You can do it this way:

你可以这样做:

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

#2


3  

Swift version:

斯威夫特版本:

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

#3


1  

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 :)

您还可以通过在按钮下的属性检查器中选择状态配置到选中或突出显示,并在那里对按钮进行样式化——不需要代码:)

#4


0  

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

or

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

This works for me.

这适合我。