Ji1*_*Bug 6 html uitextview nsattributedstring ios
我想在中显示带有<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>
Run Code Online (Sandbox Code Playgroud)
我使用以下代码在单元格中显示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;
Run Code Online (Sandbox Code Playgroud)
但UITextView它看起来像纯文本.我想得到它:样本
我该怎么做?我应该使用什么样的库?我想想HTML -> Markdown -> NSAttributedString -> TextView
Apple 的 HTML 到 AttributedText 解析器对于 CSS 非常出色,因此您实际上可以像为原始 Web 客户端那样编写它。
\n\nlet parsedCommentHTML = html.replacingOccurrences(of: "<blockquote>\\n", with: "<blockquote>\\n<k style=\\"color:#ccc; font-size: 2em; font-family: \'Copperplate\'\\">\xe2\x80\x9c</k>")\nlet blockQuoteCSS = "\\nblockquote > p {color:#808080; display: inline;} \\n blockquote { background: #f9f9f9;}"\nlet pCSS = "p {margin-bottom: 0px;}"\nlet cssStyle = "\\(blockQuoteCSS)\\n\\(pCSS)\\n"\n\nreturn 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)\nRun Code Online (Sandbox Code Playgroud)\n\n\n