44

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!

NewXcoder
  • 705
  • 1
  • 5
  • 18

8 Answers8

54

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)];
Ariel
  • 143
  • 10
PJR
  • 13,052
  • 13
  • 64
  • 104
  • 2
    When I add these code, the title become always dark blue. I think this color is the default color of UIButton, but how can it change back – NewXcoder Oct 24 '12 at 01:34
  • 16
    You can avoid typing or pasting all three lines by simply writing `[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];` – Will Andrew Jul 11 '14 at 14:15
  • 1
    @WillAndrew How do you use the `&` thing in Swift? – IIllIIll Nov 12 '15 at 22:25
  • 7
    It's weird that nobody noticed it by now, but bitwise `&` will return 0 for the mentioned code, which means that's exactly as just writing `UIControlStateNormal`. The required bitwise operator is `|`: `UIControlStateNormal | UIControlStateHighlighted | ...` – Ariel May 11 '16 at 07:28
  • 3
    currently this is wrong. Button saves different colors for different state values. for example UIControlStateSelected | UIControlStateHightLightted gives one concrete number value but UIControlStateSelected gives another concrete number value so, you should set color for differenet **compositions** of state – Nikolay Shubenkov Dec 09 '16 at 08:00
  • You have to change the button type from "System" to "Custom" in the storyboard to make it work! – Dmitry Feb 13 '20 at 19:39
  • This answer is misleading and simply wrong. Setting title color for composite state is not equivalent to setting title color for each single state separately. – The Dreams Wind Jan 26 '22 at 18:39
  • In Swift you can simply do like this: `button.setTitleColor(.yellow, for: [.normal, .highlighted, .selected])` – Starsky Jan 28 '22 at 16:31
30

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;
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
27

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

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
5

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];

Eric
  • 3,301
  • 4
  • 33
  • 39
2

watch out, system will ignore setTitleColor(_:for:) if button is not type custom.

noripcord
  • 3,412
  • 5
  • 29
  • 26
1

For something a little more reusable you might consider this, as it doesnt violate the DRY principle. Add this as a category on UIButton.

- (void)oka_setTitleColor:(UIColor *)color forStates:(NSArray *)states;
{
  [states enumerateObjectsUsingBlock:^(NSNumber *state, NSUInteger idx, BOOL *stop) {
    [self setTitleColor:color forState:[state integerValue]];
  }];
}

example usage for your case:

  [self oka_setTitleColor:[UIColor darkTextColor]
               forStates:@[@(UIControlStateNormal), @(UIControlStateHighlighted), @(UIControlStateSelected)]];
Oliver Atkinson
  • 7,970
  • 32
  • 43
  • 1
    I think the @WillAndrew's comment look better: [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected]; – JakubKnejzlik Aug 26 '14 at 12:12
0

I think mayuur is right. Have you tried another color instead of "darkTextColor" though? As far as I know "darkTextColor" is a system specific color that is used to write Text on light backgrounds. Maybe try blackColor if mayuurs suggestion doesn't work.

Edit: Try adding: [sender setHighlighted:NO]; into your IBAction which is called on button press. Does it solve it? I suggest this because from the [button release]; I guess you're still running an old version of the iOs SDK and there you don't have the option to disable the highlight of a button in an elegant way other than this.

Edit2: You're creating the button programmatically but I don't see you connecting it with an IBAction. Add this below your [[UIButton alloc] init];

[button addTarget:self action:@selector(myIBAction) forControlEvents:UIControlEventTouchUpInside];

Then create an IBAction method like this:

- (IBAction)myIBAction:(UIButton *)sender; /* In Header File */

- (IBAction)myIBAction:(UIButton *)sender{ /* In Implementation File */
        [sender setHighlighted:NO];
}
Git.Coach
  • 3,032
  • 2
  • 37
  • 54
  • Is the button correctly hooked up to the IBAction? Could you add the IBAction to your question? – Git.Coach Oct 23 '12 at 13:13
  • I creat the button in my code, and use[button addTarget:self action:@selector(continueButtonPressed) forControlEvents:UIControlEventTouchDown]; – NewXcoder Oct 23 '12 at 13:15
  • 1
    I edited my answer. I think your method won't get called if you use "TouchDown" and just tap it quickly. Try using "UIControlEventTouchUpInside" instead. – Git.Coach Oct 23 '12 at 13:19
  • I think you have got a little misunderstanding. I mean the button's title color rather than the button's color. So I think there is no business of the Highlighted. – NewXcoder Oct 24 '12 at 01:30
  • I also tried change the event to UIControlEventTouchUpInside, but it seems have no effect – NewXcoder Oct 24 '12 at 01:31
  • I understood what you meant. :) - I ran in the same problem a few weeks ago but that solved it for me... – Git.Coach Oct 24 '12 at 01:32
0

You should initialize your button with UIButtonTypeCustom instead of UIButtonTypeRoundedRect

Ramzi
  • 11
  • 3