ios 如何将按钮的文本向左对齐?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4162398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:04:08  来源:igfitidea点击:

How to alignment the text of button to the left?

iphoneios

提问by jin

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.

但它不起作用。

回答by AlexVogel

You have to use contentVerticalAlignmentand contentHorizontalAlignmentof the button.

您必须使用contentVerticalAlignmentcontentHorizontalAlignment按钮。

回答by AP_

Use ContentHorizontalAlignmentas below

使用ContentHorizontalAlignment如下

btnObj.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

回答by Metalhead1247

Swift Code

SWIFT代码

button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

回答by Aamir

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)

回答by John

Setting to button titleLable alignment also works perfectly.

设置为按钮 titleLable 对齐也很完美。

btn.titleLabel.textAlignment=NSTextAlignmentCenter;