HTML with <blockquote> tags in UITextView

陌路散爱 提交于 2019-12-04 23:27:24

问题


I want to display HTML text with <blockquote> tags in the UITextView.

example of HTML:

<div>
    <blockquote class="uncited">
        <div>
            <cite>Nick:</cite>
            <blockquote class="uncited">
                <div>
                    <cite>Tom:</cite>censored<br>
                    Hello
                </div>
             </blockquote>
             <p>World</p>
         </div>
    </blockquote>
    <p>Text</p>
</div>

I use the following code to display HTML text in the cell:

UITextView *textView = (UITextView*)[cell viewWithTag:100];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[thisComment.htmlText dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;

But in the UITextView it's looks like plain text. I want to get it: sample How can i do this? What kind of libraries should i use? i think about HTML -> Markdown -> NSAttributedString -> TextView


回答1:


Apple's HTML to AttributedText parser is excellent with CSS so you can actually write it like you'd do for a raw web client.

let parsedCommentHTML = html.replacingOccurrences(of: "<blockquote>\n", with: "<blockquote>\n<k style=\"color:#ccc; font-size: 2em; font-family: 'Copperplate'\">“</k>")
let blockQuoteCSS = "\nblockquote > p {color:#808080; display: inline;} \n blockquote { background: #f9f9f9;}"
let pCSS = "p {margin-bottom: 0px;}"
let cssStyle = "\(blockQuoteCSS)\n\(pCSS)\n"

return try NSAttributedString(data: ("<html><head><style>\(cssStyle)</style></head><span style=\"font-family: HelveticaNeue-Thin; font-size: 17\">\(CONTENT)</span></html>").data(using: String.Encoding.unicode)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)

This produces a nice (in my opinion) looking quote:



来源:https://stackoverflow.com/questions/23409819/html-with-blockquote-tags-in-uitextview

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