在TextView中高效显示html内容

在TextView中高效显示html内容,html,ios,swift,swift3,uitextview,Html,Ios,Swift,Swift3,Uitextview,我正在使用这种方法,它工作得很好,但由于使用了NSHTMLTextDocumentType,所以有一个缓慢的回退,就像我做研究一样 do { let attributedOptions:[String: Any] = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.ra

我正在使用这种方法,它工作得很好,但由于使用了NSHTMLTextDocumentType,所以有一个缓慢的回退,就像我做研究一样

do {

    let attributedOptions:[String: Any] = [
        NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue]

    let date = html.data(using: String.Encoding.utf8, allowLossyConversion: true)!

    return try NSAttributedString(data: data, options: attributedOptions , documentAttributes: nil)

} catch let error as NSError {

    print("htmo2String \(error)")
}

任何想法如何做它更快或另一种有效的方式做它

您是否尝试使用UIWebView来显示HTML内容

您可以根据自己的喜好从字符串或URL显示HTML

以下是从字符串显示HTML的示例:

string sHTMLContent = "<html><head><body><p>Hello World</p></body></head></html>";
m_WebView.LoadHtmlString(sHTMLContent , null);
string sHTMLContent=“Hello World

”; m_WebView.LoadHtmlString(sHTMLContent,null);
然后,您可以将Webview的大小设置为与具有约束的textview相等。如果需要,webview将自动滚动。

也许您可以在队列上执行解析代码

func parse(_ html: String, completionHandler: @escaping (_ attributedText: NSAttributedString?) -> (Void)) -> Void
{
    let htmlData = text.data(using: String.Encoding.utf8, allowLossyConversion: false)

    let options: [String: AnyObject] = [
        NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType as AnyObject
    ]

    completionHandler(try? NSAttributedString(data: htmlData!, options: options, documentAttributes: nil))
}
现在调用函数并等待响应

let queue: DispatchQueue = DispatchQueue(label: "com.yourcompany.Process./html_converter")

queue.async
{
    parse("<p>¡Hola mundo</p>", completionHandler: { (attributtedString: NSAttributedString?) -> (Void) in 
        if let attributtedString = attributtedString
        {
            DispatchQueue.main.async
            {
                print("str:: \(attributtedString)")
            }
        }
    })
}
let queue:DispatchQueue=DispatchQueue(标签:“com.yourcompany.Process./html\u converter”)
queue.async
{
解析(“Hola mundo

”,completionHandler:{(AttributedString:NSAttributedString?)->(Void)in 如果让AttributedString=AttributedString { DispatchQueue.main.async { 打印(“str::\(AttributedString)”) } } }) }
使用@Adolfo async idea有效显示html字符串的最终字符串扩展

具有更改字体和颜色的能力^_^

extension String {

func html2StringAsync(_ fontSize: CGFloat? = nil, color: UIColor? = nil, completionBlock:@escaping (NSAttributedString) ->()) {

    let fontSize = fontSize ?? 10

    let fontColor = color ?? UIColor.black

    let font = "Avenir !important"

    let html = "<div style=\"font-family:\(font); font-size:\(fontSize)pt; color:\(fontColor.hexString);\">" + self + "</div>"

    if let data = html.data(using: String.Encoding.utf8, allowLossyConversion: true){

        DispatchQueue.main.async {

            do {

                let attributedOptions:[String: Any] = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                       NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue]

                let attrStr = try NSAttributedString(data: data, options: attributedOptions , documentAttributes: nil)

                completionBlock(attrStr)

            } catch let error as NSError {

                print("htmo2String \(error)")
            }
        }
    }else{

        completionBlock(NSAttributedString(string: self))
    }
}
}
扩展字符串{
func html2StringAsync(ufontsize:CGFloat?=nil,color:UIColor?=nil,completionBlock:@escaping(nsattributestring)->()){
让fontSize=fontSize±10
让fontColor=color??UIColor.black
让font=“Avenir!重要”
让html=“+self+”“
如果let data=html.data(使用:String.Encoding.utf8,allowLossyConversion:true){
DispatchQueue.main.async{
做{
let AttributeOptions:[String:Any]=[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:String.Encoding.utf8.rawValue]
让attrStr=try NSAttributedString(数据:数据,选项:attributedOptions,文档属性:nil)
完成块(attrStr)
}将let错误捕获为NSError{
打印(“htmo2String\(错误)”)
}
}
}否则{
completionBlock(NSAttribute字符串(字符串:self))
}
}
}

如果您更好地理解这个问题,他会询问有效的方法。或者缓存:使用异步解析将其保存到html来源的对象