Uibutton Title Text Color

UIButton title text color

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)

How to set the title text color of UIButton?

You have to use func setTitleColor(_ color: UIColor?, for state: UIControl.State) the same way you set the actual title text. Docs

isbeauty.setTitleColor(UIColorFromRGB("F21B3F"), for: .normal)

How to change the title text color of UI Button?

For example - white title text, gray when highlighted (tap is down):

isbeauty.setTitleColor(.white, for: .normal)
isbeauty.setTitleColor(.gray, for: .highlighted)

Set UIButton text color programmatically

Don’t set the label’s color directly; use -setTitleColor:forState:.

button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)

How can I change UIButton title color?

You can use -[UIButton setTitleColor:forState:] to do this.

Example:

Objective-C

[buttonName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

Swift 2

buttonName.setTitleColor(UIColor.blackColor(), forState: .Normal)

Swift 3

buttonName.setTitleColor(UIColor.white, for: .normal)

Thanks to richardchildan

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

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

Why am I not able to change UIButton text-color?

Issue

Setting tintColor works only for buttonType = .custom. When you create a button using UIButton() the default buttonType would be .system which will override your global titleLabel and tintColor due to configuration with specific states

Solution

If you want the behavior of the system UIButton of fading the titleLabel when pressing the button, you need to set the titleColor for each state like below:

let button = UIButton()
button.setTitleColor(UIColor.red, for: .normal)

Otherwise you can use .custom and set tintColor directly.

let button = UIButton(type: .custom)
button.tintColor = UIColor.red

UIButton Change Title Color Partially

Use NSMutableAttributedString for this purpose

private let signUpButton = UIButton().then {
// Creating gray text part
let grayButtonText = NSAttributedString(string: "FirstPart", attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.gray ])
// Creating black text part
let blackButtonText = NSAttributedString(string: "SecondPart", attributes: [.font: UIFont.systemFont(ofSize: 12), .foregroundColor: UIColor.black ])

// Merging two parts together
let buttonTitle = NSMutableAttributedString(attributedString: grayButtonText)
buttonTitle.append(blackButtonText)

// Set it as a title for ".normal" state
$0.setAttributedTitle(buttonTitle, for: .normal)
}


Related Topics



Leave a reply



Submit