NSAttributedString alignment not working on html content

蹲街弑〆低调 提交于 2019-12-01 05:22:26

The issue is that as said by the doc initWithData:options:documentAttributes:error: only accept 3 possibles keys: NSDocumentTypeDocumentAttribute, NSCharacterEncodingDocumentAttribute and NSDefaultAttributesDocumentAttribute.

Then, with your snippet, you didn't applied the NSParagraphStyleAttributedName at the end like in your previous attempt.

So, from your snippet, I put this line instead:

[attributedText addAttribute:NSParagraphStyleAttributeName
                       value:paragraphStyle
                       range:NSMakeRange(0, attributedText.length)];

And removed the 2 useless attributed from attr:

NSDictionary *attr = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                       NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};

Well, I found a solution. I assume the alignment doesn't work because it expects this kind of attributes in the html. So I added it to the html:

text = [@"<style> p {text-align: right;}</style><body>" stringByAppendingFormat:@"<p>%@</p></body>", text];

Works with center, left, etc.

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