UIButton - Save text when touched

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

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

How to keep the text the same black so that when you touch the button only the image changes.

+6
source share
3 answers

In most cases, the following line will be executed:

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

If nothing happens, 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 the comments below because this answer has been edited / expanded to include two separate lines.

+12
source

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

+5
source
 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]; } 
0
source

Source: https://habr.com/ru/post/897027/


All Articles