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

Hie*_*ham 4 setattribute swift nsparagraphstyle xcode-6.2 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)
Run Code Online (Sandbox Code Playgroud)

但是我在这条线上遇到了问题:

paragraphStyle.alignment = NSTextAlignment.Center
Run Code Online (Sandbox Code Playgroud)

我有错误: Cannot assign to 'alignment' in 'paragraphStyle'

如何设置对齐WKInterfaceLabel使用setAttributedText

aya*_*aio 5

您需要使用NSMutableParagraphStyle

var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.CenterTextAlignment
Run Code Online (Sandbox Code Playgroud)