Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton title color change on highlight - How to turn it off?

I have created a button. The title's color is black by default. But when I press it, the color changes to be a little blue and never changes back again, how does this happen? Can anyone tell me why? And I want the button's title remain black all the time. How can I do that? I have tried

[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateSelected];

But There is no effect. When I add this in my code, it seems the button's title always blue.

Code as follows.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 360, 280, 44)];
[button setTitle:NSLocalizedString(@"Continue", @"Label: TextLabel in Continue button") forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
button.titleLabel.textColor = [UIColor darkTextColor];
button.titleLabel.shadowColor = [UIColor blackColor];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;

[self.view addSubview:button];
[button release];

Thanks everyone. I have sloved the problem. I think the root cause is

button.titleLabel.textColor = [UIColor darkTextColor];

When I remove this, and use

button setTitleColor:(UIColor) forState:(UIControlState);

The problem is solved!

like image 960
NewXcoder Avatar asked Oct 23 '12 12:10

NewXcoder


4 Answers

you can use

[UIButton setTitleColor:forState:]

for all the states , then title color will remain same for all states.

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

Note:To avoide type or paste above code three times you can use following code suggested by Will,

[button setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted | UIControlStateNormal | UIControlStateSelected)];
like image 189
PJR Avatar answered Nov 12 '22 23:11

PJR


As @null points out, by far the simplest way to do this is to set the button type in Interface Builder (or in code) to "Custom".

If you need to replicate this behavior with a standard button, override the setHighlighted method to prevent the alpha channel of the titleLabel from adjusting too:

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    self.titleLabel.alpha = 1.0;
}
like image 29
brandonscript Avatar answered Nov 13 '22 00:11

brandonscript


0 lines of code:

Using Interface Builder and either .XIB or .storyboard, select your UIButton in IB:
View > Utilities > Show Attributes Inspector.

Select State Config (Default) to one of Highlighted, Selected or Disabled and change the Text Color attribute.

Interface Builder solution

like image 27
SwiftArchitect Avatar answered Nov 12 '22 23:11

SwiftArchitect


There are a few comments pointing this out, but in order to have it as an actual answer:

Set the button type to Custom in your storyboard or in code: [UIButton buttonWithType:UIButtonTypeCustom];

like image 5
Eric Avatar answered Nov 12 '22 23:11

Eric