How to change Button Title Alignment in Swift?
Solution 1:
Use contentHorizontalAlignment
.You have to use UIControlContentHorizontalAlignment.Left
.You need to usehorizontal
not vertical
.
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
Swift 3.x
btn.contentHorizontalAlignment = .left
Solution 2:
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
Solution 3:
btnAddress.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
btnAddress.contentVerticalAlignment = UIControl.ContentVerticalAlignment.top
And now if you want the text to have some space from left add this
btnAddress.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)