ios UILabel 将文本居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5722730/
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 19:38:13  来源:igfitidea点击:

UILabel Align Text to center

iostextalignmentuilabel

提问by vivianaranha

How do I align text in UILabel?

如何对齐文本UILabel

回答by Aravindhan

From iOS 6and later UITextAlignmentis deprecated. use NSTextAlignment

iOS 6及更高版本UITextAlignment已弃用。用NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Swift Version from iOS 6 and later

iOS 6 及更高版本的 Swift 版本

myLabel.textAlignment = .center

回答by Vishal Shah

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

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

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

You can read more about it here UILabel

你可以在这里阅读更多关于它的信息UILabel

回答by AdamT

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

要在斯威夫特一个UILabel(这是针对于iOS 7+)中心的文字,你可以这样做:

myUILabel.textAlignment = .Center

Or

或者

myUILabel.textAlignment = NSTextAlignment.Center

回答by John Parker

N.B.: As per the UILabel class reference, as of iOS 6 this approach is now deprecated.

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

Simply use the textAlignmentproperty to see the required alignment using one of the UITextAlignmentvalues. (UITextAlignmentLeft, UITextAlignmentCenteror UITextAlignmentRight.)

只需使用该textAlignment属性即可使用其中一个UITextAlignment值查看所需的对齐方式。( UITextAlignmentLeft,UITextAlignmentCenterUITextAlignmentRight.)

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

例如: [myUILabel setTextAlignment:UITextAlignmentCenter];

See the UILabel Class Referencefor more information.

有关更多信息,请参阅UILabel 类参考

回答by Jayprakash Dubey

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

使用yourLabel.textAlignment = NSTextAlignmentCenter;针对iOS> = 6.0和yourLabel.textAlignment = UITextAlignmentCenter;为iOS <6.0。

回答by Micha? Kwiecień

For Swift 3 it's:

对于 Swift 3,它是:

label.textAlignment = .center

回答by Rinju Jain

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

IO6.1 [lblTitle setTextAlignment:NSTextAlignmentCenter];

回答by Mubin Shaikh

Label.textAlignment = NSTextAlignmentCenter;

回答by Dilip Jangid

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

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

title.TextAlignment = UITextAlignment.Center;

回答by iOS

In Swift 4.2and Xcode 10

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)