jin
jin

Reputation: 2155

How to alignment the text of button to the left?

Now I want to alignment the text of button to the left with code , how to do ? and I code this :

button.titleLabel.textAlignment = UITextAlignmentLeft;

but it doesn't work.

Upvotes: 41

Views: 30877

Answers (5)

John
John

Reputation: 258

Setting to button titleLable alignment also works perfectly.

btn.titleLabel.textAlignment=NSTextAlignmentCenter;

Upvotes: 0

Aamir
Aamir

Reputation: 17007

For those who are using Swift 3

button.contentHorizontalAlignment = .left

Above code align text to left border, so you might want little padding.

button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)

Upvotes: 2

AP_
AP_

Reputation: 1043

Use ContentHorizontalAlignment as below

btnObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

Upvotes: 26

Metalhead1247
Metalhead1247

Reputation: 1978

Swift Code

button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

Upvotes: 9

AlexVogel
AlexVogel

Reputation: 10621

You have to use contentVerticalAlignment and contentHorizontalAlignment of the button.

Upvotes: 79

Related Questions