我想用 <blockquote>
显示 HTML 文本UITextView
中的标签.
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
最佳答案
Apple 的 HTML 到 AttributedText 解析器非常适合 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)
关于UITextView 中带有 <blockquote> 标签的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23409819/