2015-08-13 15 views
12

Tôi có một chuỗi chứa HTML. Tôi muốn hiển thị HTML trong một điều khiển TextView. Tôi tìm thấy một số mã và thử nó:Cách hiển thị văn bản HTML trong TextView

def = "some html text" 

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

Trên lựa chọn tôi nhận được một lỗi:

[String: String] is not convertible to string.

Ai đó có thể giúp tôi hiển thị HTML trong một TextView?

+0

Dường như NSAttributedString được trả lại một mảng hai chiều. Tôi nghĩ bạn sẽ phải lặp lại thông qua điều tra. – ouflak

Trả lời

39

Điều này phù hợp với tôi. Hãy nhớ rằng các nhà xây dựng NSAttributedString tại throws một đối tượng NSError:

Swift 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) 
} 
+0

Khi tôi sử dụng mã này, tôi nhận được thông báo sau trong bảng điều khiển: "+ [Giao dịch đồng bộ hóa] được gọi trong giao dịch." Bạn có biết làm sao để sửa cái này không? –

+0

@EvanKaminsky Hmm, không chắc chắn nếu không có thêm ngữ cảnh. Bạn có thể hỏi một câu hỏi mới cho thấy nơi bạn đang gọi điều này và chuỗi HTML là gì? – JAL

+0

Tôi tin rằng đó là vấn đề tương tự như http://stackoverflow.com/questions/28457998/swift-catransaction-synchronize-called-within-transaction-while-decoding-htm, nhưng giải pháp hiện tại sử dụng thư viện bên ngoài. –

0

Hãy thử SwiftSoup. Công việc này cho tôi.

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() 
0

Cố gắng sử dụng phiên bản Swift3 của mã mà tôi tìm thấy ở đây:

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 
Các vấn đề liên quan