185

I'm setting text color for UIButton

headingButton.titleLabel.textColor = [UIColor colorWithRed:36/255.0 
                                                     green:71/255.0 
                                                      blue:113/255.0 
                                                     alpha:1.0];

It's not changing color same code I'm using in another code it's working.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Ali
  • 1,951
  • 2
  • 14
  • 14
  • possible duplicate of [How can I change UIButton title color?](http://stackoverflow.com/questions/2474289/how-can-i-change-uibutton-title-color) – sKhan May 29 '15 at 23:05

5 Answers5

472

use

Objective-C

[headingButton setTitleColor:[UIColor colorWithRed:36/255.0 green:71/255.0 blue:113/255.0 alpha:1.0] forState:UIControlStateNormal];

Swift

headingButton.setTitleColor(.black, for: .normal)
Hexfire
  • 5,945
  • 8
  • 32
  • 42
Amit Singh
  • 8,383
  • 4
  • 28
  • 31
  • 2
    this works. but the code in the original question also often works. anyone know why the title only sometimes fails to respond to the code in the question? – crgt Dec 03 '13 at 04:41
  • 7
    The reason is that when the button is clicked or otherwise changes state, the code in the button resets the titleLabel's properties to match the internal settings the button has for that state. – psycotica0 Jan 17 '14 at 17:03
  • 1
    the documentation of apple states the following : "Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes." No real explanation of the "why" though. – clauswey Mar 04 '15 at 12:12
  • MOST IMPORTANTLY when you set a color for UIControlStateNormal, there is no need to set it for all the other states (if you want your button to look the same when pressed). – drpawelo Dec 11 '15 at 14:59
  • how to change button text color globally in app delegate. Maybe using UIButton.appearance().setTitleColor? – Awais Fayyaz Apr 05 '18 at 10:31
6

I created a custom class MyButton extended from UIButton. Then added this inside the Identity Inspector:

enter image description here

After this, change the button type to Custom:

enter image description here

Then you can set attributes like textColor and UIFont for your UIButton for the different states:

enter image description here

Then I also created two methods inside MyButton class which I have to call inside my code when I want a UIButton to be displayed as highlighted:

- (void)changeColorAsUnselection{
    [self setTitleColor:[UIColor colorFromHexString:acColorGreyDark] 
               forState:UIControlStateNormal & 
                        UIControlStateSelected & 
                        UIControlStateHighlighted];
}

- (void)changeColorAsSelection{
    [self setTitleColor:[UIColor colorFromHexString:acColorYellow] 
               forState:UIControlStateNormal & 
                        UIControlStateHighlighted & 
                        UIControlStateSelected];
}

You have to set the titleColor for normal, highlight and selected UIControlState because there can be more than one state at a time according to the documentation of UIControlState. If you don't create these methods, the UIButton will display selection or highlighting but they won't stay in the UIColor you setup inside the UIInterface Builder because they are just available for a short display of a selection, not for displaying selection itself.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
5

In Swift:

Changing the label text color is quite different than changing it for a UIButton. To change the text color for a UIButton use this method:

self.headingButton.setTitleColor(UIColor(red: 107.0/255.0, green: 199.0/255.0, blue: 217.0/255.0), forState: UIControlState.Normal)
AaoIi
  • 8,288
  • 6
  • 45
  • 87
2

swift 5 version:

By using default inbuilt color:

  1. button.setTitleColor(UIColor.green, for: .normal)

OR

You can use your custom color by using RGB method:

  1. button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)
NickCoder
  • 1,504
  • 2
  • 23
  • 35
1

Besides de color, my problem was that I was setting the text using textlabel

bt.titleLabel?.text = title

and I solved changing to:

bt.setTitle(title, for: .normal)
Ronaldo Albertini
  • 1,329
  • 20
  • 24