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.
use
Objective-C
Swift
In Swift:
Changing the label text color is quite different than changing it for a
UIButton
. To change the text color for aUIButton
use this method:I created a custom class MyButton extended from
UIButton
. Then added this inside theIdentity Inspector
:After this, change the button type to Custom:
Then you can set attributes like
textColor
andUIFont
for yourUIButton
for the different states:Then I also created two methods inside
MyButton
class which I have to call inside my code when I want aUIButton
to be displayed as highlighted:You have to set the
titleColor
for normal, highlight and selectedUIControlState
because there can be more than one state at a time according to the documentation ofUIControlState
. If you don't create these methods, theUIButton
will display selection or highlighting but they won't stay in theUIColor
you setup inside theUIInterface Builder
because they are just available for a short display of a selection, not for displaying selection itself.By using default inbuilt color:
button.setTitleColor(UIColor.green, for: .normal)
OR
You can use your custom color by using RGB method:
button.setTitleColor(UIColor(displayP3Red: 0.0/255.0, green: 180.0/255.0, blue: 2.0/255.0, alpha: 1.0), for: .normal)