swiftsetattributexcode-6.2nsparagraphstylewkinterfacelabel

How to set alignment for WKInterface Label using setAttributedText


I'm trying to set alignment for WKInterfaceLabel using setAttributedText function. Here is my code:

 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)

But I got a problem with this line:

paragraphStyle.alignment = NSTextAlignment.Center

I got error: Cannot assign to 'alignment' in 'paragraphStyle'

How to set alignment for WKInterfaceLabel using setAttributedText?


Solution

  • You need to use NSMutableParagraphStyle:

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