4

I would like to display the String HTML in a UITextView control using swift. I found some code and tried it:

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

but i gives some error i am unable to understand or found any solution, which states as,

Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'

I need to figure out why i am having this error. Any solution or alternate code? The other solutions are in objective-c if they either have answers they are giving the same error i tried many just because of old versions.

Shehroz Saleem
  • 196
  • 2
  • 15
  • 2
    Possible duplicate of [Display html text in uitextview](https://stackoverflow.com/questions/2454067/display-html-text-in-uitextview) – Hexfire Dec 21 '17 at 09:56
  • Swift 4 introduced new "keys": NSDocumentTypeDocumentAttribute vs `NSAttributedString.someKey` You are using the old one, and what looks like more of Objective-C one. – Larme Dec 21 '17 at 09:57

4 Answers4

7

Try This

var attrStr = try! NSAttributedString(
            data: "<b><i>text</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
            options:[NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
        YOUR_TEXTVIEW.attributedText = attrStr
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
2

Change options to

let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]

So it should be:

 do {
    let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
    let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: options, documentAttributes: nil)
} catch {
    print(error)
}
kamwysoc
  • 6,709
  • 2
  • 34
  • 48
0

Use this, it is properly working for me

let attrStr = try! NSAttributedString(
            data: htmlText.data(using: .unicode, allowLossyConversion: true)!,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
            NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
            documentAttributes: nil)
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey' already explained in question. – Shehroz Saleem Dec 21 '17 at 10:08
0

Try this

   let htmlString = "<html><body> Some html string </body></html>";

    do {
        let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)

        self.txView.attributedText  = str

    } catch {
        print(error)
    }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87