How to Set the Title of a Uibutton as Left-Aligned

How can I set the title of a UIButton as left-aligned?

Set the contentHorizontalAlignment:

// Swift 
emailBtn.contentHorizontalAlignment = .left;

// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

You might also want to adjust the content left inset otherwise the text will touch the left border:

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

// Objective-C
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

Swift UiButton how to left align the title?

Try this one

button.contentHorizontalAlignment = .left

Trouble left-aligning UIButton title (iOS/Swift)

Try this:

button.contentHorizontalAlignment = .left

How to left align title on button using titleLabel property?

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.

How to change Button Title Alignment in Swift?

Use contentHorizontalAlignment.You have to use UIControlContentHorizontalAlignment.Left.You need to usehorizontal not vertical.

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

Swift 3.x

btn.contentHorizontalAlignment = .left

Setting title alignment to left on button

The text is being left-aligned. The problem is that the title label (within which the text is left-aligned) is itself located in the middle of the button!

So, what you want to do is adjust the position of the label within the button. There are various ways to do this. For example, look at the button's contentHorizontalAlignment.

How to center attributed title in UIButton

This is because the text is an attributed String, you will need to align the text after it has been set.

Solution for you will be:

aButton.setAttributedTitle(title, for: .normal)
aButton.titleLabel?.textAlignment = .center

By setting attributed String, instead of String, you are clearing all the button's text property.

Nicer solution will be to add alignment attribute into the attributed String



Related Topics



Leave a reply



Submit