Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change attributed title of UIButton

Tags:

ios

uibutton

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!

like image 520
Markus Avatar asked Sep 13 '14 09:09

Markus


People also ask

How do I change the button title color in Objective C?

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)

How do I change the button title color in Swift?

Is-it possible to change the color of the Title for the button ? Usually, use setTitleColor(_:for:) because buttons can have different UIControlState : . normal , .

What is NSAttributedString?

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.

How to create a new UIButton programmatically?

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.

How to customize the font of a UIButton?

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.

How to set text on a button in uicontrol?

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

How to make the button title bold or italic?

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.


2 Answers

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.

like image 59
Goppinath Avatar answered Sep 30 '22 17:09

Goppinath


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)
}
like image 26
Danny Avatar answered Sep 30 '22 17:09

Danny