4

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?

M Zubair Shamshad
  • 2,741
  • 3
  • 23
  • 45
Hieu Duc Pham
  • 1,074
  • 1
  • 10
  • 24

1 Answers1

5

You need to use NSMutableParagraphStyle:

var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.CenterTextAlignment
Eric Aya
  • 69,473
  • 35
  • 181
  • 253