How to change Button Title Alignment in Swift?

derdida picture derdida · Jul 29, 2014 · Viewed 54.1k times · Source

I am trying to set the title on a Button to left. But everything i tried is not working.

With:

UILabelButton.titleLabel.textAlignment = .Left

Or:

UILabelButton.contentVerticalAlignment = UIControlContentVerticalAlignment // There is no left

The button title is still centered.

Is there another Property?

Answer

codester picture codester · Jul 29, 2014

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

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

Swift 3.x

btn.contentHorizontalAlignment = .left