uibutton text color on touch - CoursesEssaydatesCom

uibutton text color on touch

Stack Overflow



  1. Log In
    Sign Up

  2. current community


    • Stack Overflow

      help
      chat

    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct .

Join Stack Overflow to learn, share knowledge, and build your career.

Email Sign Up
Sign Up

or sign in with


Google

Facebook

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

Ask Question


up vote
36
down vote

favorite

2

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!

ios uibutton title textcolor
share | improve this question

edited Oct 24 ’12 at 1:46

asked Oct 23 ’12 at 12:57

NewXcoder

3101314

  • 1

    What method is this button linked to?
    –  esqew
    Oct 23 ’12 at 12:59

  • Do you want to disable the highlighting image? In Xib file, uncheck "highlight adjusts image".
    –  mayuur
    Oct 23 ’12 at 13:01

  • So far I did not implement the related method.
    –  NewXcoder
    Oct 23 ’12 at 13:06

  • 8

    Change the button type from "System" to "Custom" in your storyboard.
    –  autremoi
    Sep 16 ’14 at 2:54

add a comment  | 

6 Answers
6

active

oldest

votes


up vote
53
down vote

accepted

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

share | improve this answer

edited May 11 ’16 at 8:12

Ariel

1039

answered Oct 23 ’12 at 13:27

PJR

9,571115392

  • 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 1:34

  • 15

    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

  • 5

    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 7:28


  • 2

    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 9 ’16 at 8:00


add a comment  | 


up vote
23
down vote

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;

share | improve this answer

edited Feb 4 ’17 at 17:26

answered Jun 4 ’14 at 23:50

brandonscript

34.2k2199156

  • 47

    You should change the UIButton type from "System" to "Custom" it will do just that.
    –  null
    Sep 3 ’14 at 6:57

  • Hey…. the more you know!
    –  brandonscript
    Sep 3 ’14 at 7:24

  • But if you do like @remus said, before all your custom stuff, add [super setHighlighted:highlighted];
    –  StinkyCat
    Jan 12 ’15 at 12:09

  • 1

    but this will cause it to briefly ‘flash’
    –  Ralph Marczynski
    Nov 2 ’15 at 22:26

add a comment  | 


up vote
20
down vote

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

share | improve this answer

edited Feb 5 ’16 at 2:51

answered Apr 1 ’15 at 6:38

SwiftArchitect

29.3k1396132

add a comment  | 


up vote
2
down vote

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

share | improve this answer

answered Jun 28 ’16 at 19:14

Eric

1,71832637

add a comment  | 


up vote
1
down vote

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

share | improve this answer

answered May 14 ’13 at 9:58

Oliver Atkinson

6,3662136

  • 1

    I think the @WillAndrew’s comment look better: [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
    –  JakubKnejzlik
    Aug 26 ’14 at 12:12

add a comment  | 


up vote
0
down vote

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;
- (IBAction)myIBAction:(UIButton *)sender [sender setHighlighted:NO];

share | improve this answer

edited Oct 23 ’12 at 13:17

answered Oct 23 ’12 at 13:06

Thomas Johannesmeyer

1,79812443

  • I have tried, but it seems not work! Also thanks
    –  NewXcoder
    Oct 23 ’12 at 13:11


  • Is the button correctly hooked up to the IBAction? Could you add the IBAction to your question?
    –  Thomas Johannesmeyer
    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.
    –  Thomas Johannesmeyer
    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 1:30

 | 
show 2 more comments

Not the answer you’re looking for? Browse other questions tagged ios uibutton title textcolor or ask your own question .

asked

5 years, 10 months ago

viewed

38,176 times

active

1 year, 6 months ago

Linked

0

Custom type UIButton with image only highlights image when touched

Related

1078

How can I disable the UITableView selection highlighting?

900

How to change the name of an iOS app?

167

How can I change UIButton title color?

414

How to set the title of UIButton as left alignment?

0

iPhone Xcode UIButton Title Will Not Appear

148

UIButton title text color

0

Monotouch UIView Appearance changing UITextField background colors, UIButton background colors, etc

201

How to change the background color of a UIButton while it’s highlighted?

926

How to change Status Bar text color in iOS

1

Change Button Color on state change

Hot Network Questions

  • How do you explain to a 5th grader why division by zero is meaningless?

  • Bookshelf Arrangement Dilemma

  • Why does there seem to be a lack of conservative comedy and comedy-news compared to liberal?

  • Where did Percy sleep when he was away from the Weasleys?

  • Why didn’t the Spacecraft used for the Apollo 11 mission melt in the Earth’s Atmosphere?

  • Only two train sets available?

  • Can a knight move through all squares from its original position?

  • In a vacuum can a cooler body radiate Infrared radiation to a warmer body?

  • Planetary Defense in an Interstellar Society

  • How to compare two typenames for equality in C++?

  • What happens if you "unlearn" Animate Dead?

  • I2C bus occupied

  • Tower of strings

  • Non-constant infinite arithmetic progressions?

  • How can prostitution be made acceptable in a religiously conservative society?

  • Is the "FirstOntario Centre" "in Toronto"?

  • Where did Dumbledore live while working at Hogwarts?

  • Why do some games persistently have mostly one viable strategy, while others can have many?

  • Why did I have to wave my hand in front of my ID card?

  • How to handle dungeons involving only one party member?

  • Custom question lists is available for testing – why?

  • Short story with retired selfdriving cars

  • Why do ex-government employees have security clearances?

  • Have phone scammers recorded "Yes" statements to fraudulently authorise payments?

more hot questions


question feed

default

Stack Overflow works best with JavaScript enabled

Stack Overflow



  1. Log In
    Sign Up

  2. current community


    • Stack Overflow

      help
      chat

    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct .

Join Stack Overflow to learn, share knowledge, and build your career.

Email Sign Up
Sign Up

or sign in with


Google

Facebook

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

Ask Question


up vote
36
down vote

favorite

2

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!

ios uibutton title textcolor
share | improve this question

edited Oct 24 ’12 at 1:46

asked Oct 23 ’12 at 12:57

NewXcoder

3101314

  • 1

    What method is this button linked to?
    –  esqew
    Oct 23 ’12 at 12:59

  • Do you want to disable the highlighting image? In Xib file, uncheck "highlight adjusts image".
    –  mayuur
    Oct 23 ’12 at 13:01

  • So far I did not implement the related method.
    –  NewXcoder
    Oct 23 ’12 at 13:06

  • 8

    Change the button type from "System" to "Custom" in your storyboard.
    –  autremoi
    Sep 16 ’14 at 2:54

add a comment  | 

6 Answers
6

active

oldest

votes


up vote
53
down vote

accepted

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

share | improve this answer

edited May 11 ’16 at 8:12

Ariel

1039

answered Oct 23 ’12 at 13:27

PJR

9,571115392

  • 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 1:34

  • 15

    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

  • 5

    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 7:28


  • 2

    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 9 ’16 at 8:00


add a comment  | 


up vote
23
down vote

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;

share | improve this answer

edited Feb 4 ’17 at 17:26

answered Jun 4 ’14 at 23:50

brandonscript

34.2k2199156

  • 47

    You should change the UIButton type from "System" to "Custom" it will do just that.
    –  null
    Sep 3 ’14 at 6:57

  • Hey…. the more you know!
    –  brandonscript
    Sep 3 ’14 at 7:24

  • But if you do like @remus said, before all your custom stuff, add [super setHighlighted:highlighted];
    –  StinkyCat
    Jan 12 ’15 at 12:09

  • 1

    but this will cause it to briefly ‘flash’
    –  Ralph Marczynski
    Nov 2 ’15 at 22:26

add a comment  | 


up vote
20
down vote

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

share | improve this answer

edited Feb 5 ’16 at 2:51

answered Apr 1 ’15 at 6:38

SwiftArchitect

29.3k1396132

add a comment  | 


up vote
2
down vote

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

share | improve this answer

answered Jun 28 ’16 at 19:14

Eric

1,71832637

add a comment  | 


up vote
1
down vote

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

share | improve this answer

answered May 14 ’13 at 9:58

Oliver Atkinson

6,3662136

  • 1

    I think the @WillAndrew’s comment look better: [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
    –  JakubKnejzlik
    Aug 26 ’14 at 12:12

add a comment  | 


up vote
0
down vote

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;
- (IBAction)myIBAction:(UIButton *)sender [sender setHighlighted:NO];

share | improve this answer

edited Oct 23 ’12 at 13:17

answered Oct 23 ’12 at 13:06

Thomas Johannesmeyer

1,79812443

  • I have tried, but it seems not work! Also thanks
    –  NewXcoder
    Oct 23 ’12 at 13:11


  • Is the button correctly hooked up to the IBAction? Could you add the IBAction to your question?
    –  Thomas Johannesmeyer
    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.
    –  Thomas Johannesmeyer
    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 1:30

 | 
show 2 more comments

Not the answer you’re looking for? Browse other questions tagged ios uibutton title textcolor or ask your own question .

asked

5 years, 10 months ago

viewed

38,176 times

active

1 year, 6 months ago

Linked

0

Custom type UIButton with image only highlights image when touched

Related

1078

How can I disable the UITableView selection highlighting?

900

How to change the name of an iOS app?

167

How can I change UIButton title color?

414

How to set the title of UIButton as left alignment?

0

iPhone Xcode UIButton Title Will Not Appear

148

UIButton title text color

0

Monotouch UIView Appearance changing UITextField background colors, UIButton background colors, etc

201

How to change the background color of a UIButton while it’s highlighted?

926

How to change Status Bar text color in iOS

1

Change Button Color on state change

Hot Network Questions

  • sh recursive copy (cp -r) – How to exclude subfolder

  • In a vacuum can a cooler body radiate Infrared radiation to a warmer body?

  • What do you call the act of drinking a whole bottle of, say, water in one go?

  • Planetary Defense in an Interstellar Society

  • Only two train sets available?

  • Is it possible for an ancient contraption to survive modern-day attempts of breaking in?

  • Where did Dumbledore live while working at Hogwarts?

  • Bookshelf Arrangement Dilemma

  • Why don’t bond makers just get loans?

  • How do I communicate to my players that a door is, for the time being, absolutely locked to them?

  • Why is peer review so random?

  • A word or phrase for ‘enemy of faith’

  • Can you sneak into Premium Economy Class?

  • How can I help a close friend get over me?

  • Why does Samsung include useless capacitors?

  • How would one attack or lay siege to a flying castle?

  • Finding the standard deviation of a probability distribution.

  • How can I make my girlfriend not to get fixated on false facts and listen for reasons?

  • I2C bus occupied

  • Can I forbid the government from using my patented invention?

  • Why were floppy disks invented after hard disks?

  • How to let users decide between transactions

  • What do we call "a person who blames others for things they didn’t do"?

  • How to handle dungeons involving only one party member?

more hot questions


question feed

default

Stack Overflow works best with JavaScript enabled