中心证明UILabel文本?

时间:2015-04-20 17:13:49

标签: ios swift

我试图将我的UILabel文本中心对齐,如图:

enter image description here

我的代码示例:

myLabel.textAlignment = .Justified myLabel.lineBreakMode = .ByWordWrapping

我的结果是:

enter image description here

我错过了什么吗?

2 个答案:

答案 0 :(得分:5)

我在那里找到答案:https://stackoverflow.com/a/27548566/833816

通过添加paragraphStyle.firstLineHeadIndent = 0.001

来工作

完整样本:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Justified
paragraphStyle.firstLineHeadIndent = 0.001

let mutableAttrStr = NSMutableAttributedString(attributedString: detailsLabel.attributedText)
mutableAttrStr.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, mutableAttrStr.length))
myLabel.attributedText = mutableAttrStr

答案 1 :(得分:0)

尝试

myLabel.textAlignment = NSTextAlignment.Center