UIButton - Keep text black when touched
I have an iPhone UIButton (Custom) whic开发者_开发百科h 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.
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.
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]
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];
}
精彩评论