cheznead
cheznead

Reputation: 2779

How to left align title on button using titleLabel property?

I tried to use:

button.titleLabel?.textAlignment = .left

to left align the text on a button, but the text still appears in the centre. I don't have any code to set the title of the button to the centre of the button so it must do that by default.

Can anyone tell me how to use the textAlignment property on the titleLabel of the button for left alignment?

Edit: To address the proposals to close the question, you can if you see fit to do so, but my question is not the same as simply asking how to left align the text in a button. I asked how to use a specific property (titleLabel) to do so, which I believe has not been asked before. And I received a quality answer which answered my question.

Upvotes: 0

Views: 317

Answers (1)

luk2302
luk2302

Reputation: 57114

You cannot really / should not really use the titleLabel property for this since the UIButton decides how to layout the label, where to position it and with what size, etc. If the button decides to put the label in the center and size it to the minimal dimensions it needs to display the text then changing any alignment property of the label will have no effect.

button.contentHorizontalAlignment = .left

is the way to go. Do not change the integrated label, but the button.

Upvotes: 1

Related Questions