在UITextView中带有标记的HTML
我想在UITextView
显示带有
标签的HTML文本。
HTML的例子:
Nick:Tom:censored
HelloWorld
Text
我使用以下代码在单元格中显示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
它看起来像纯文本。 我想得到它: 样本我该怎么做? 我应该使用什么样的库? 我想到HTML -> Markdown -> NSAttributedString -> TextView
Apple的HTML到AttributedText解析器非常适合CSS,因此您可以像对待原始Web客户端一样编写它。
let parsedCommentHTML = html.replacingOccurrences(of: "
\n", with: "\n“ ") 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: ("\(CONTENT)").data(using: String.Encoding.unicode)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)这产生了一个很好的(在我看来)看起来引用: