I want to programmatically change the title of an UIButton
which has an attributed title.
The button is created in IB. I do not want to change the attributes just the title/text.
I've tried the code below but cannot find a way to change the title of the NSAttributedString
.
NSAttributedString *attributedString = [self.deleteButton attributedTitleForState:UIControlStateNormal];
// How can I change title of attributedString without changing the attributes?
[self.deleteButton setAttributedTitle:attributedString forState:UIControlStateNormal];
Thank you!
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)
Is-it possible to change the color of the Title for the button ? Usually, use setTitleColor(_:for:) because buttons can have different UIControlState : . normal , .
An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.
To create a new UIButton, set its width, height, and position the button within a view programmatically, you can do with CGRect class. For example: Setting text on a button is very simple. It can be done with a function call setTitle (_ title: String?, for state: UIControl.State) . A title on the button can be set for different button states.
There are lots of different customization you can with UIButton. For example, you might be also interested to learn how to create UIButton and change its style, background color, tint color and make it call a function when tapped. To set a different font on UIButton programmatically you will need to create a new instance of UIFont object.
Set Button Title Text Setting text on a button is very simple. It can be done with a function call setTitle (_ title: String?, for state: UIControl.State). // Set text on button
You can also use a simple system font style to make your button title bold. In the code snippet below I will create a new button and will make it use a bold system font style. You can also make the button title look italic. In the code snippet below I will create a new button and will make it use an italic system font style.
Partially you have the answer.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:[_ deleteButton attributedTitleForState:UIControlStateNormal]];
[attributedString replaceCharactersInRange:NSMakeRange(0, attributedString.length) withString:@"Your new string"];
[_ deleteButton setAttributedTitle:attributedString forState:UIControlStateNormal];
Instead of creating NSAttributedString
create NSMutableAttributedString
then you can just set the string like this.
Swift 3 answer:
if let attributedTitle = yourButton.attributedTitle(for: .normal) {
let mutableAttributedTitle = NSMutableAttributedString(attributedString: attributedTitle)
mutableAttributedTitle.replaceCharacters(in: NSMakeRange(0, mutableAttributedTitle.length), with: "New title")
yourButton.setAttributedTitle(mutableAttributedTitle, for: .normal)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With