ios 如何在 Swift 中更改按钮标题对齐方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25005659/
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
How to change Button Title Alignment in Swift?
提问by derdida
I am trying to set the title on a Button to left. But everything i tried is not working.
我试图将按钮上的标题设置为左侧。但我尝试的一切都不起作用。
With:
和:
UILabelButton.titleLabel.textAlignment = .Left
Or:
或者:
UILabelButton.contentVerticalAlignment = UIControlContentVerticalAlignment // There is no left
The button title is still centered.
按钮标题仍然居中。
Is there another Property?
是否还有其他物业?
回答by codester
Use contentHorizontalAlignment.You have to use UIControlContentHorizontalAlignment.Left.You need to usehorizontalnot vertical.
使用contentHorizontalAlignment。你必须使用UIControlContentHorizontalAlignment.Left。你需要使用horizontal不vertical。
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
Swift 3.x
斯威夫特 3.x
btn.contentHorizontalAlignment = .left
回答by Vivek
Swift 4
斯威夫特 4
Sample code
示例代码
For my requirement I want alignment lefttop
对于我的要求,我想对准左顶部
btnAddress.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
btnAddress.contentVerticalAlignment = UIControlContentVerticalAlignment.top
Swift 5 update:
斯威夫特 5 更新:
btnAddress.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
btnAddress.contentVerticalAlignment = UIControl.ContentVerticalAlignment.top
回答by e-nature
like this with swift 4:
像这样的swift 4:
btnAddress.contentHorizontalAlignment = .center
回答by Noman Haroon
btnAddress.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
btnAddress.contentVerticalAlignment = UIControl.ContentVerticalAlignment.top
And now if you want the text to have some space from left add this
现在如果你想让文本从左边有一些空间添加这个
btnAddress.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
btnAddress.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)

