Cómo mostrar text HTML en TextView

Tengo una cadena que contiene HTML. Me gustaría mostrar el HTML en un control TextView. Encontré un código y lo probé:

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

En la opción obtengo un error:

[String: String] no es convertible a cadena.

¿Alguien puede ayudarme a mostrar HTML en un TextView?

Esto funciona para mí. Recuerde que el constructor NSAttributedString ahora throws un object 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) } 

Prueba SwiftSoup . Esto me funciona.

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

Intenta utilizar la versión Swift3 del código que encontré aquí:

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