How to alignment the text of button to the left?

2020-05-21 07:40发布

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.

标签: iphone ios
5条回答
Emotional °昔
2楼-- · 2020-05-21 08:13

You have to use contentVerticalAlignment and contentHorizontalAlignment of the button.

查看更多
forever°为你锁心
3楼-- · 2020-05-21 08:14

Use ContentHorizontalAlignment as below

btnObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
查看更多
成全新的幸福
4楼-- · 2020-05-21 08:22

Swift Code

button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
查看更多
相关推荐>>
5楼-- · 2020-05-21 08:29

Setting to button titleLable alignment also works perfectly.

btn.titleLabel.textAlignment=NSTextAlignmentCenter;
查看更多
够拽才男人
6楼-- · 2020-05-21 08:33

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)
查看更多
登录 后发表回答