How to set UIButton header text color?

I tried changing the color of the text for the button, but it still remains white.

isbeauty = UIButton() isbeauty.setTitle("Buy", forState: UIControlState.Normal) isbeauty.titleLabel?.textColor = UIColorFromRGB("F21B3F") isbeauty.titleLabel!.font = UIFont(name: "AppleSDGothicNeo-Thin" , size: 25) isbeauty.backgroundColor = UIColor.clearColor() isbeauty.layer.cornerRadius = 5 isbeauty.layer.borderWidth = 1 isbeauty.layer.borderColor = UIColorFromRGB("F21B3F").CGColor isbeauty.frame = CGRectMake(300, 134, 55, 26) isbeauty.addTarget(self,action: "first:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(isbeauty) 

I also tried changing it to red, black, blue, but nothing happens.

+101
ios swift uibutton
Jun 27 '15 at 11:20
source share
5 answers

You should use func setTitleColor(_ color: UIColor?, forState state: UIControlState) in the same way that you set the actual title text. Documents

 isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), forState: .Normal) 
+223
Jun 27 '15 at 11:24
source share

Swift UI Solution

 Button(action: {}) { Text("Button") }.foregroundColor(Color(red: 1.0, green: 0.0, blue: 0.0)) 

Swift 3, Swift 4, Swift 5

improve comments. This should work:

 button.setTitleColor(.red, for: .normal) 
+100
Jan 25 '17 at 14:18
source share

Button header color setting example

 btnDone.setTitleColor(.black, for: .normal) 
+6
Feb 21 '17 at 3:50
source share

This is a quick 5 compatible answer. If you want to use one of the built-in colors, then you can simply use

 button.setTitleColor(.red, for: .normal) 

If you need some custom colors, then create an extension for UIColor, as shown below.

 import UIKit extension UIColor { static var themeMoreButton = UIColor.init(red: 53/255, green: 150/255, blue: 36/255, alpha: 1) } 

Then use it for your button as shown below.

 button.setTitleColor(UIColor.themeMoreButton, for: .normal) 

Hint: You can use this method to store custom colors from the rgba color code and reuse them in the application.

+2
Apr 05 '19 at 4:25
source share
 func setTitleColor(_ color: UIColor?, for state: UIControl.State) 

Parameters :

color:
The color of the header to use in the specified state.

state:
A state that uses the specified color. Possible values ​​are described in UIControl.State.

An example :

 let MyButton = UIButton() MyButton.setTitle("Click Me..!", for: .normal) MyButton.setTitleColor(.green, for: .normal) 
+2
Apr 11 '19 at 9:37
source share



All Articles