带有UITextView </blockquote>中的<blockquote>标签的HTML

时间:2014-05-01 14:34:01

标签: html ios uitextview nsattributedstring

我想在<blockquote>。{/ p>中显示带有UITextView标记的HTML文字

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>

我使用以下代码在单元格中显示HTML文本:

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;

但在UITextView中,它看起来像纯文本。我想得到它:sample 我怎样才能做到这一点?我应该使用什么样的库?我想到HTML -> Markdown -> NSAttributedString -> TextView

1 个答案:

答案 0 :(得分:1)

Apple的AttributedText解析器的HTML非常适合CSS,因此您可以像对待原始Web客户端一样编写它。

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)

这会产生一个很好的(在我看来)看起来很棒: enter image description here