gpt4 book ai didi

UITextView 中带有
标签的 HTML

转载 作者:太空狗 更新时间:2023-10-29 15:14:03 26 4
gpt4 key购买 nike

我想用 <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)

这会产生一个漂亮的(在我看来)引述: enter image description here

关于UITextView 中带有 <blockquote> 标签的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23409819/

26 4 0