Development Tip

How to change Button Title Alignment in Swift?

yourdevel 2021. 1. 9. 10:59
반응형

How to change Button Title Alignment in Swift?


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?


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

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

Swift 3.x

btn.contentHorizontalAlignment = .left

Swift 4

Sample code

For my requirement I want alignment left top

btnAddress.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
btnAddress.contentVerticalAlignment = UIControlContentVerticalAlignment.top

Swift 5 update:

btnAddress.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
btnAddress.contentVerticalAlignment = UIControl.ContentVerticalAlignment.top

like this with swift 4: btnAddress.contentHorizontalAlignment = .center

ReferenceURL : https://stackoverflow.com/questions/25005659/how-to-change-button-title-alignment-in-swift

반응형