如何使用setAttributedText为WKInterface Label设置对齐方式

时间:2015-04-20 08:35:19

标签: swift setattribute xcode-6.2 nsparagraphstyle wkinterfacelabel

我正在尝试使用WKInterfaceLabel函数设置setAttributedText的对齐方式。这是我的代码:

 var paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
 paragraphStyle.alignment = NSTextAlignment.Center
 var attributedDictonary = [NSForegroundColorAttributeName:UIColor.greenColor(), NSParagraphStyleAttributeName:paragraphStyle]
 var attributeString = NSAttributedString(string: "TextAttributed", attributes: attributedDictonary)
 self.titleLabel.setAttributedText(attributeString)

但是这条线路出了问题:

paragraphStyle.alignment = NSTextAlignment.Center

我收到错误:Cannot assign to 'alignment' in 'paragraphStyle'

如何使用WKInterfaceLabel设置setAttributedText的对齐方式?

1 个答案:

答案 0 :(得分:4)

您需要使用NSMutableParagraphStyle

var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.CenterTextAlignment