开发者

How to alignment the text of button to the left?

开发者 https://www.devze.com 2023-01-24 12:25 出处:网络
Now I want to alignmentthe text of button to the left with code 开发者_运维百科, how to do ? and I code this :

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消