Now I want to alignment the text of button to the left with code , how to do ? and I code this :
现在我想用左边的按钮文本对齐代码,该怎么办?我编码这个:
button.titleLabel.textAlignment = UITextAlignmentLeft;
but it doesn't work.
但它不起作用。
5 个解决方案
#1
70
You have to use contentVerticalAlignment
and contentHorizontalAlignment
of the button.
您必须使用按钮的contentVerticalAlignment和contentHorizontalAlignment。
#2
23
Use ContentHorizontalAlignment
as below
使用ContentHorizontalAlignment如下所示
btnObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
#3
9
Swift Code
SWIFT代码
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
#4
1
For those who are using Swift 3
对于那些使用Swift 3的人
button.contentHorizontalAlignment = .left
Above code align text to left border, so you might want little padding.
上面的代码将文本与左边框对齐,因此您可能需要少量填充。
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
#5
0
Setting to button titleLable alignment also works perfectly.
设置为按钮titleLable对齐也完美。
btn.titleLabel.textAlignment=NSTextAlignmentCenter;
#1
70
You have to use contentVerticalAlignment
and contentHorizontalAlignment
of the button.
您必须使用按钮的contentVerticalAlignment和contentHorizontalAlignment。
#2
23
Use ContentHorizontalAlignment
as below
使用ContentHorizontalAlignment如下所示
btnObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
#3
9
Swift Code
SWIFT代码
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
#4
1
For those who are using Swift 3
对于那些使用Swift 3的人
button.contentHorizontalAlignment = .left
Above code align text to left border, so you might want little padding.
上面的代码将文本与左边框对齐,因此您可能需要少量填充。
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
#5
0
Setting to button titleLable alignment also works perfectly.
设置为按钮titleLable对齐也完美。
btn.titleLabel.textAlignment=NSTextAlignmentCenter;