Ian Vink
Ian Vink

Reputation: 68800

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.

Upvotes: 6

Views: 2986

Answers (3)

chown
chown

Reputation: 52778

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.

Upvotes: 12

karim
karim

Reputation: 15589

okButton.titleLabel.textColor = [UIColor redColor];
[okButton addTarget:self action:@selector(isPressingForgetButton:) forControlEvents:UIControlEventTouchDown];
[okButton addTarget:self action:@selector(didPressForgetButton:) forControlEvents:UIControlEventTouchUpInside];


- (void) isPressingForgetButton:(id)sender
{
    UIButton * bt = (UIButton *)sender;
    bt.titleLabel.textColor = [UIColor greenColor];
}
- (void) didPressForgetButton:(id)sender
{
    UIButton * bt = (UIButton *)sender;
    bt.titleLabel.textColor = [UIColor redColor];
    [self gotoUnblockView];
}

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185771

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

Upvotes: 5

Related Questions