UILabel 将文本居中对齐
如何对齐 UILabel
中的文本?
How do I align text in UILabel
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何对齐 UILabel
中的文本?
How do I align text in UILabel
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
从 iOS 6 及更高版本开始,
UITextAlignment
已被弃用。使用NSTextAlignment
iOS 6 及更高版本的 Swift 版本
From iOS 6 and later
UITextAlignment
is deprecated. useNSTextAlignment
Swift Version from iOS 6 and later
下面是一个示例代码,展示了如何使用 UILabel 对齐文本:
您可以在此处阅读更多相关信息 UILabel
Here is a sample code showing how to align text using UILabel:
You can read more about it here UILabel
要将文本在 Swift 中的 UILabel 中居中(针对 iOS 7+),您可以执行以下操作:
或者
To center text in a UILabel in Swift (which is targeted for iOS 7+) you can do:
Or
注意:根据 UILabel 类参考,从 iOS 6 开始,这种方法现已被弃用。
只需使用
textAlignment
属性即可使用以下之一查看所需的对齐方式:UITextAlignment
值。 (UITextAlignmentLeft
、UITextAlignmentCenter
或UITextAlignmentRight
。)例如:
[myUILabel setTextAlignment:UITextAlignmentCenter];
请参阅 UILabel 类参考 了解更多信息。
N.B.: As per the UILabel class reference, as of iOS 6 this approach is now deprecated.
Simply use the
textAlignment
property to see the required alignment using one of theUITextAlignment
values. (UITextAlignmentLeft
,UITextAlignmentCenter
orUITextAlignmentRight
.)e.g.:
[myUILabel setTextAlignment:UITextAlignmentCenter];
See the UILabel Class Reference for more information.
对于 iOS >= 6.0 使用
yourLabel.textAlignment = NSTextAlignmentCenter;
,对于 iOS= UITextAlignmentCenter;
使用yourLabel.textAlignment = UITextAlignmentCenter;
6.0。Use
yourLabel.textAlignment = NSTextAlignmentCenter;
for iOS >= 6.0 andyourLabel.textAlignment = UITextAlignmentCenter;
for iOS < 6.0.对于 Swift 3 来说是:
For Swift 3 it's:
IO6.1
[lblTitle setTextAlignment:NSTextAlignmentCenter];
IO6.1
[lblTitle setTextAlignment:NSTextAlignmentCenter];
在 xamarin ios 中
假设您的标签名称是 title 然后执行以下操作
In xamarin ios
suppose your label name is title then do the following
在 Swift 4.2 和 Xcode 10 中
In Swift 4.2 and Xcode 10