UILabel 将文本居中对齐

发布于 2024-11-02 05:53:45 字数 38 浏览 4 评论 0原文

如何对齐 UILabel 中的文本?

How do I align text in UILabel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

り繁华旳梦境 2024-11-09 05:53:45

iOS 6 及更高版本开始,UITextAlignment 已被弃用。使用 NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

iOS 6 及更高版本的 Swift 版本

myLabel.textAlignment = .center

From iOS 6 and later UITextAlignment is deprecated. use NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Swift Version from iOS 6 and later

myLabel.textAlignment = .center
月亮是我掰弯的 2024-11-09 05:53:45

下面是一个示例代码,展示了如何使用 UILabel 对齐文本:

label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 12)];
label.textAlignment = NSTextAlignmentCenter;

您可以在此处阅读更多相关信息 UILabel

Here is a sample code showing how to align text using UILabel:

label = [[UILabel alloc] initWithFrame:CGRectMake(60, 30, 200, 12)];
label.textAlignment = NSTextAlignmentCenter;

You can read more about it here UILabel

白芷 2024-11-09 05:53:45

要将文本在 Swift 中的 UILabel 中居中(针对 iOS 7+),您可以执行以下操作:

myUILabel.textAlignment = .Center

或者

myUILabel.textAlignment = NSTextAlignment.Center

To center text in a UILabel in Swift (which is targeted for iOS 7+) you can do:

myUILabel.textAlignment = .Center

Or

myUILabel.textAlignment = NSTextAlignment.Center
后eg是否自 2024-11-09 05:53:45

注意:根据 UILabel 类参考,从 iOS 6 开始,这种方法现已被弃用。

只需使用 textAlignment 属性即可使用以下之一查看所需的对齐方式: UITextAlignment 值。 (UITextAlignmentLeftUITextAlignmentCenterUITextAlignmentRight。)

例如:[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 the UITextAlignment values. (UITextAlignmentLeft, UITextAlignmentCenter or UITextAlignmentRight.)

e.g.: [myUILabel setTextAlignment:UITextAlignmentCenter];

See the UILabel Class Reference for more information.

段念尘 2024-11-09 05:53:45

对于 iOS >= 6.0 使用 yourLabel.textAlignment = NSTextAlignmentCenter;,对于 iOS = UITextAlignmentCenter; 使用 yourLabel.textAlignment = UITextAlignmentCenter; 6.0。

Use yourLabel.textAlignment = NSTextAlignmentCenter; for iOS >= 6.0 and yourLabel.textAlignment = UITextAlignmentCenter; for iOS < 6.0.

岛歌少女 2024-11-09 05:53:45

对于 Swift 3 来说是:

label.textAlignment = .center

For Swift 3 it's:

label.textAlignment = .center
路弥 2024-11-09 05:53:45

IO6.1
[lblTitle setTextAlignment:NSTextAlignmentCenter];

IO6.1
[lblTitle setTextAlignment:NSTextAlignmentCenter];

我们只是彼此的过ke 2024-11-09 05:53:45
Label.textAlignment = NSTextAlignmentCenter;
Label.textAlignment = NSTextAlignmentCenter;
孤千羽 2024-11-09 05:53:45

在 xamarin ios 中
假设您的标签名称是 title 然后执行以下操作

title.TextAlignment = UITextAlignment.Center;

In xamarin ios
suppose your label name is title then do the following

title.TextAlignment = UITextAlignment.Center;
夏末 2024-11-09 05:53:45

Swift 4.2 和 Xcode 10 中

let lbl = UILabel(frame: CGRect(x: 10, y: 50, width: 230, height: 21))
lbl.textAlignment = .center //For center alignment
lbl.text = "This is my label fdsjhfg sjdg dfgdfgdfjgdjfhg jdfjgdfgdf end..."
lbl.textColor = .white
lbl.backgroundColor = .lightGray//If required
lbl.font = UIFont.systemFont(ofSize: 17)

 //To display multiple lines in label
lbl.numberOfLines = 0
lbl.lineBreakMode = .byWordWrapping

lbl.sizeToFit()//If required
yourView.addSubview(lbl)

In Swift 4.2 and Xcode 10

let lbl = UILabel(frame: CGRect(x: 10, y: 50, width: 230, height: 21))
lbl.textAlignment = .center //For center alignment
lbl.text = "This is my label fdsjhfg sjdg dfgdfgdfjgdjfhg jdfjgdfgdf end..."
lbl.textColor = .white
lbl.backgroundColor = .lightGray//If required
lbl.font = UIFont.systemFont(ofSize: 17)

 //To display multiple lines in label
lbl.numberOfLines = 0
lbl.lineBreakMode = .byWordWrapping

lbl.sizeToFit()//If required
yourView.addSubview(lbl)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文