我如何在UILabel对齐文本?


当前回答

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

title.TextAlignment = UITextAlignment.Center;

其他回答

对于Swift 3,它是:

label.textAlignment = .center

IO6.1 [lblTitle setTextAlignment NSTextAlignmentCenter):;

从iOS 6及以后,UITextAlignment已弃用。使用NSTextAlignment

myLabel.textAlignment = NSTextAlignmentCenter;

Swift版本来自iOS 6及更高版本

myLabel.textAlignment = .center

在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)

要在Swift的UILabel中居中文本(这是针对iOS 7+),你可以做:

myUILabel.textAlignment = .Center

Or

myUILabel.textAlignment = NSTextAlignment.Center