如何以编程方式使 iOS 文本标签居中对齐?
我想设置文本标签的对齐方式,该怎么做?
I want to set the alignment of a text label, how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想设置文本标签的对齐方式,该怎么做?
I want to set the alignment of a text label, how can I do that?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
我认为这些答案对您有帮助。正确的方法是:
有关更多文档,您可以阅读以下内容:
https://developer.apple.com/documentation/uikit/uilabel
在 Swift 中:-
这里
.center
是NSTextAlignment.center
I think there are the answers who helped you out. The correct way to do this is:
for more documentation you can read this:
https://developer.apple.com/documentation/uikit/uilabel
In Swift :-
Here
.center
isNSTextAlignment.center
在这里,
编辑
如果您的目标是 iOS6 以上,请使用
NSTextAlignmentCenter
,因为UITextAlignmentCenter
已被弃用希望它有帮助。
Here you are,
EDIT
if you target above iOS6 use
NSTextAlignmentCenter
asUITextAlignmentCenter
is depreciatedHope it helps.
从 iOS 6.0 开始,这一情况已发生变化,UITextAlignment 已被弃用。现在正确的方法是:
这是 NSTextAlignment 枚举,它提供了文本对齐选项:
Objective-C:
Swift:
来源
This has changed as of iOS 6.0, UITextAlignment has been deprecated. The correct way to do this now is:
Here is the NSTextAlignment enumerable that gives the options for text alignment:
Objective-C:
Swift:
Source
请参阅 UILabel 文档
see UILabel documentation
如果你有一个多行 UILabel 你应该使用 NSMutableParagraphStyle
If you have a multiline UILabel you should use a NSMutableParagraphStyle
在 Swift 3 及更高版本中应该是
In Swift 3 and beyond it should be
在斯威夫特4:
in swift 4: