iOS: UIButton titleLabel -- does it do anything at all? iOS: UIButton titleLabel -- does it do anything at all? ios ios

iOS: UIButton titleLabel -- does it do anything at all?


Setting the titleLabel's text property like that has no effect. Instead, call -setTitle:forState: on the button:

[editButton setTitle:@"Edit" forState:UIControlStateNormal]

The reason for this is because the button can have different titles for different states (e.g., UIControlStateDisabled, UIControlStateHighlighted). Setting a property for the UIControlStateNormal control state will apply to all the states if you don't specify the others explicitly.

Per the documentation for UIButton:

This class provides methods for setting the title, image, and other appearance properties of a button. By using these accessors, you can specify a different appearance for each button state.

You can customize label's color and shadow color based on the state. See -setTitleColor:forState and -setTitleShadowColor:forState, respectively. The rest of the properties on titleLabel, such as textAlignment and font, should work as you have them set now and should apply to all the control states.

Specifically, see the documentation for UIButton's titleLabel property: https://developer.apple.com/documentation/uikit/uibutton/1623992-titlelabel

titleLabel itself is read-only, but that doesn't mean you can't change the values of its own properties, such as font, line break mode, etc.


Here's how I worked it out using Swift 4.2.

counterButton.setTitle("Start Counter",                      for:UIControl.State.normal)


matomo