UITextView 中带有 <blockquote> 标签的 HTML

标签 html ios uitextview nsattributedstring

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

相关文章:

javascript - 如何获取拖放目标div的父ID?

ios - Swift如何实时替换UITextView文本的多个字符?

html - 使用 CSS 按顺序扩展圆圈

javascript - 使用javascript播放声音不起作用

ios - Google Play 服务 - Unity - 在 iOS 上只能邀请 'recent players'

iphone - 如何配置 iPhone/iPad 文件与 Xcode 4.3 的关联?

iOS 11 : UITextView typingAttributes reset when typing

ios - UITextView selectAll 方法未按预期工作

javascript - 在俄罗斯方 block 之类的游戏中,碰撞后物体不显示

ios - 在 Swift 中使用泛型进行类型推断