Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton - Keep text black when touched

I have an iPhone UIButton (Custom) which has a background image and text.

When touched, the image darkens (Good) but the text goes from the set Black to white.

How do I keep the text the same black so that when the button is touched, only the image changes color.

like image 740
Ian Vink Avatar asked Sep 10 '11 00:09

Ian Vink


2 Answers

Most of the times, the following line will do:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

If nothing happens, then use either this:

[button setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected | UIControlStateHighlighted | UIControlStateNormal)];

Or this will do the trick:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];

See comments below for reason this answer was edited/expanded to include the 2 separate lines.

like image 156
chown Avatar answered Sep 18 '22 22:09

chown


[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]

like image 30
Lily Ballard Avatar answered Sep 18 '22 22:09

Lily Ballard