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.
You have to use contentVerticalAlignment
and contentHorizontalAlignment
of the button.
Use ContentHorizontalAlignment
as below
btnObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Swift Code
button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
For those who are using 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)
Setting to button titleLable alignment also works perfectly.
btn.titleLabel.textAlignment=NSTextAlignmentCenter;
精彩评论