NSAttributedString alignment not working on html content

前端 未结 2 1279
醉梦人生
醉梦人生 2021-01-12 18:39

Want to change alignment of html label. Nothing works. I don\'t see any CSS in the HTML. There are no further settings changing the alignment. I also set left alignment dire

相关标签:
2条回答
  • 2021-01-12 19:09

    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)};
    
    0 讨论(0)
  • 2021-01-12 19:15

    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.

    0 讨论(0)
提交回复
热议问题