Changing UIButton text

99,172

Solution 1

When laying out its subviews, a UIButton will set its titleLabel's text value using its own title values, so that you can set up to four different strings for the four states (normal, highlighted, selected, disabled).

Because of this feature, setting the titleLabel's text directly won't persist, and will be reset by the button when it lays out its subviews.

This is what you have to do to change the title text for a button's state.

[calibrationButton setTitle:@"Calibration" forState:UIControlStateNormal];

Solution 2

To set button text use the following method:

[calibrationButton setTitle: @"Calibration" forState: UIControlStateNormal];

See UIButton class reference for more details... http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

Or in Swift 3:

calibrationButton.setTitle("Calibration", for: .normal)

Solution 3

programmatically you can set button title like below:

[myButton setTitle:@"buttonTitle" forState:UIControlStateNormal];

you can also set button title property from storyboard.

Solution 4

Not a huge deal, and possibly obvious, but there are several states available for buttons. If you provide the 'wrong' one, you will not see the text change as desired.

I noticed that my button was not showing the text I added, using the methods shown here. Check this link to make sure you are providing the UIControlState that you intend.

What's the difference between UIControlStateHighlighted and UIControlStateSelected?

Solution 5

For Swift 3.0

let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
button.setTitle("set here", for: .normal)
button.addTarget(self, action: #selector(TableViewController.actionButtonTocuh), for: .touchUpInside)
button.titleLabel?.textColor  = #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1)
view.addSubview(button)
Share:
99,172

Related videos on Youtube

Julian Coltea
Author by

Julian Coltea

Updated on July 21, 2022

Comments

  • Julian Coltea
    Julian Coltea almost 2 years

    So I'm trying to update the text on a UIButton when I click it. I'm using the following line to change the text:

    calibrationButton.titleLabel.text = @"Calibration";
    

    I have verified that the text is changing, but when I run the app and I click on the button, it changes to "Calibration" for a split second and then goes right back to its default value. Any ideas why this might be happening? Is there some sort of refresh function I need to be calling?

  • Boris Gafurov
    Boris Gafurov about 11 years
    so what button.titleLabel.text=@"some text" is supposed to do then?
  • erdekhayser
    erdekhayser almost 11 years
    @BorisGafurov If you type in button.titleLabel, you see that titleLabel is a read-only property, and so any of a read-only property's properties are read-only as well. Using dot-notation to edit them won't work, and so you need explicit methods in order to edit them. This makes sense for me at least.
  • race_carr
    race_carr over 10 years
    I think I wasted about two hours until I realized this (which sadly I think I already knew!)
  • dinesharjani
    dinesharjani over 9 years
    Thanks for your answer.
  • Aaron
    Aaron over 9 years
    @erdekhayser Actually, read-only properties CAN have read/write properties. I use the titlelabel's tag to pass an integer to a action method. Also see Apple's docs link Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:
  • erdekhayser
    erdekhayser over 9 years
    @Aaron Yes you are right. I'm slightly embarrassed that I wrote that, but that was in the first couple months that I was into iOS, so I guess I can't be too concerned
  • Aaron
    Aaron over 9 years
    @erdekhayser no worries! it's an old post.