2015-08-13 6 views
12

Mam ciąg znaków zawierający HTML. Chciałbym wyświetlić HTML w formancie TextView. Znalazłem jakiś kod i próbował go:Jak wyświetlić tekst HTML w TextView

def = "some html text" 

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

na opcji pojawia się błąd:

[String: String] is not convertible to string.

Czy ktoś może mi pomóc wyświetlić HTML w TextView?

+0

to wygląda NSAttributedString wraca dwuwymiarową tablicę. Myślę, że będziesz musiał powtórzyć numerację. – ouflak

Odpowiedz

39

To działa dla mnie. Pamiętaj, że konstruktor NSAttributedString teraz throwsNSError obiektu:

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

Kiedy używam tego kodu, otrzymuję następujący komunikat w konsoli: "+ [Katransakcja synchronizuję] wywoływane w ramach transakcji." Czy wiesz, jak to naprawić? –

+0

@EvanKaminsky Hmm, nie jestem pewien bez większego kontekstu. Czy możesz zadać nowe pytanie pokazujące, gdzie to nazywasz i jaki jest łańcuch HTML? – JAL

+0

Wierzę, że jest to ten sam problem, co http://stackoverflow.com/questions/28457998/swift-catransaction-synchronize-called-within-transaction-while-decoding-htm, ale obecne rozwiązanie wykorzystuje bibliotekę zewnętrzną. –

0

Spróbuj SwiftSoup. Ta praca dla mnie.

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

Spróbuj użyć wersji Swift3 kodu, który znalazłem tutaj:

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