问题
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:
You need to use NSMutableParagraphStyle
:
var paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.CenterTextAlignment
来源:https://stackoverflow.com/questions/29742863/how-to-set-alignment-for-wkinterface-label-using-setattributedtext