How to set alignment for WKInterface Label using setAttributedText

﹥>﹥吖頭↗ 提交于 2019-12-22 10:48:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!