我试图将我的UILabel文本中心对齐,如图:
我的代码示例:
myLabel.textAlignment = .Justified
myLabel.lineBreakMode = .ByWordWrapping
我的结果是:
我错过了什么吗?
答案 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