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
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)
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 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)
iOS button title color won't change
Because your button will turn back to UIControlState.Normal
after you touch it, it become .Highlighted
, then .Normal
You should set sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
to
sender.setTitleColor(UIColor.whiteColor(), forState: UIControlState. Normal)
or, just set it for all the states since u did a check for color like
sender.setTitleColor(UIColor.whiteColor(), forState: [.Normal,.Selected,.Highlighted])
*Edit: Above doesn't work, can use NSAttributedString
instead
if self.loginButton.backgroundColor == UIColor.blackColor() {
let tittle = NSAttributedString(string: "Login", attributes: [NSForegroundColorAttributeName: UIColor.blackColor()])
loginButton.setAttributedTitle(tittle, forState: .Normal)
loginButton.backgroundColor = UIColor.whiteColor()
} else {
let tittle = NSAttributedString(string: "Login", attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
loginButton.setAttributedTitle(tittle, forState: .Normal)
loginButton.backgroundColor = UIColor.blackColor()
}
change the background color of the title in UIButton
The full answer is :
btn.tintColor = UIColor.clear //if we have different tint, which I had
btn.titleLabel?.backgroundColor = UIColor.clear
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
Set UIButton text color programmatically
Don’t set the label’s color directly; use -setTitleColor:forState:
.
button.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
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
How Secure Is Nsuserdefaults on iOS 8,9
iOS 11 Uitableview Delete Rows Animation Bug
iOS Static Framework with Resources Inside
Can't Find Pods.Modulemap - Looking in Wrong Directory
Uiimagepickercontroller Allowsediting Not Working
Why My App Is Not Shown in Icloud Drive Folder
iOS App Won't Run on Device Any More Under iOS 12: Unrecoverable Ct Signature Issue
How to Animate a Uibutton Between Two Png Images
Can Siri Be Invoked Programmatically Within My App Using Private APIs
Printing the View in iOS with Swift
How to Center Crop an Image in Swiftui