How to set alignment for WKInterface Label using setAttributedText

811 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

You need to use NSMutableParagraphStyle:

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