1

I need to convert the HTML code to an NSAttributeString and then display it with a UITextView.

Because I need to do a reading typesetting engine, I think WebView does not handle paging problems very well.

I tried using the following code to convert the hr tag, but there is no display on the UITextView

let htmlStr = "<hr>"
let htmlData = htmlStr.data(using: .unicode)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
let attributeStr = try? NSAttributedString(data: htmlData, options: options, documentAttributes: nil)
textView.attributedText = attributeStr

So, how can I solve this problem? Still have a better typography engine idea?

Coder
  • 43
  • 7
  • In HTML 5, the `
    ` tag doesn't represent a line, it's a "thematic break". The code you posted creates an attributed string with a paragraph break so technically it's converting correctly. If you actually want a line, see [Draw a line inside a UITextView - NSAttributedString](https://stackoverflow.com/questions/35942355/draw-a-line-inside-a-uitextview-nsattributedstring)
    – rmaddy Apr 11 '19 at 18:11
  • Thank you so much, If I want to change the color of the line, what should I do with NSAttributeString? – Coder Apr 12 '19 at 12:37

0 Answers0