带< blockquote>的HTML标记在UITextView中 [英] HTML with <blockquote> tags in UITextView

查看:132
本文介绍了带< blockquote>的HTML标记在UITextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UITextView 中显示带有< blockquote> 标签的HTML文本。



HTML示例:

 < div> 
< blockquote class =uncited>
< div>
< cite> Nick:< / cite>
< blockquote class =uncited>
< div>
< cite> Tom:< / cite>审查< br>
Hello
< / div>
< / blockquote>
< p>世界< / p>
< / div>
< / blockquote>
< p>文字< / 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 中它看起来像纯文本。我想得到它:


I want to display HTML text with <blockquote> tags in the UITextView.

example of 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>

I use the following code to display HTML text in the cell:

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;

But in the UITextView it's looks like plain text. I want to get it: sample How can i do this? What kind of libraries should i use? i think about HTML -> Markdown -> NSAttributedString -> TextView

解决方案

Apple's HTML to AttributedText parser is excellent with CSS so you can actually write it like you'd do for a raw web client.

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)

This produces a nice (in my opinion) looking quote:

这篇关于带&lt; blockquote&gt;的HTML标记在UITextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆