【问题标题】:How to display HTML text in TextView如何在 TextView 中显示 HTML 文本
【发布时间】:2015-08-13 18:21:01
【问题描述】:

我有一个包含 HTML 的字符串。我想在 TextView 控件中显示 HTML。我找到了一些代码并尝试了它:

def = "some html text"

definition.attributedText = NSAttributedString(
  data: def.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
  options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
      documentAttributes: nil,
      error: nil)

在选项上我得到一个错误:

[String: String] 不能转换为字符串。

有人可以帮我在 TextView 中显示 HTML 吗?

【问题讨论】:

  • 看起来 NSAttributedString 正在返回一个二维数组。我认为您将不得不遍历枚举。

标签: html ios swift swift2 uitextview


【解决方案1】:

这对我有用。请记住,NSAttributedString 构造函数现在是 throws 一个 NSError 对象:

斯威夫特 3:

do {
    let str = try NSAttributedString(data: def.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
    print(error)
}

Swift 2.x:

do {
    let str = try NSAttributedString(data: def.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
    print(error)
}

【讨论】:

  • 当我使用此代码时,我在控制台中收到以下消息:“+[CATransaction synchronize] 在事务中调用。”你知道如何解决这个问题吗?
  • @EvanKaminsky 嗯,在没有更多上下文的情况下不确定。您能否提出一个新问题,说明您在哪里调用它以及 HTML 字符串是什么?
  • 我相信这和stackoverflow.com/questions/28457998/…是同一个问题,但目前的解决方案是使用外部库。
  • @EvanKaminsky 有趣,我以前从未见过。我会看看我是否可以重现该问题并调查修复。
  • 我的 html 中有一个
【解决方案2】:

试试SwiftSoup。这对我有用。

let html = "<html><head><title>First parse</title></head><body><p>Parsed HTML into a doc.</p></body></html>"
let doc: Document = try SwiftSoup.parse(html)
let text: String = try doc.text()

【讨论】:

    【解决方案3】:

    尝试使用我在此处找到的代码的 Swift3 版本:

    https://github.com/codepath/objc_ios_guides/wiki/Generating-NSAttributedString-from-HTML

                func styledHTMLwithHTML(_ HTML: String) -> String {
                    let style: String = "<meta charset=\"UTF-8\"><style> body { font-family: 'HelveticaNeue'; font-size: 20px; } b {font-family: 'MarkerFelt-Wide'; }</style>"
                    return "\(style)\(HTML)"
                }
    
                func attributedString(withHTML HTML: String) -> NSAttributedString {
                    let options: [AnyHashable: Any] = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
                    return try! NSAttributedString(data: HTML.data(using: String.Encoding.utf8)!, options: options as! [String : Any], documentAttributes: nil)
                }
    
                // This is a string that you might find in your model
                var html: String = "This is <b>bold</b>"
    
                // Apply some inline CSS
                var styledHtml: String = styledHTMLwithHTML(html)
    
                // Generate an attributed string from the HTML
                var attributedText: NSAttributedString = attributedString(withHTML: styledHtml)
    
                // Set the attributedText property of the UILabel
                label.attributedText = attributedText
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      相关资源
      最近更新 更多