如何更改快速按钮标题对齐方式?

我试图设置一个按钮的标题左。但我尝试的一切都没有工作。

配合:

UILabelButton.titleLabel.textAlignment = .Left

或者:

UILabelButton.contentVerticalAlignment = UIControlContentVerticalAlignment // There is no left

按钮标题仍然居中。

还有别的房产吗?

76000 次浏览

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
    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)

The Most Easiest and smallest Solution:

Button("0"){}.frame(width:160.0,height:70.0,alignment:Alignment.leading)

Check this by using this code for a button you will see that button text is now aligned to "LEFT"

Button with text Alignment LEFT