有效地在TextView中显示html内容

时间:2017-01-31 14:42:38

标签: html ios swift swift3 uitextview

我使用这种方式并且工作正常,但由于使用了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)")
}

任何想法如何更快或另一种有效的方式来做到这一点!

3 个答案:

答案 0 :(得分:2)

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

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)")
            }
        }
    })
}

答案 1 :(得分:0)

您是否尝试使用UIWebView呈现HTML内容?

您可以根据需要显示字符串或网址中的HTML。

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

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

然后,您可以使用约束将Webview的大小设置为等于textview。如果需要,webview将自动滚动。

答案 2 :(得分:0)

使用@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))
    }
}
}