0

I read several posts and all suggested using the following extension, but it's not working:

extension String {
    func htmlAttributedString() -> NSAttributedString? {
        guard let data = self.data(using: .utf8) else {
            return nil
        }

        return try? NSAttributedString(
            data: data,
            options: [.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil
        )
    }
}

This is how I use it:

enter image description here

At runtime I get this error:

enter image description here

Laith
  • 409
  • 6
  • 13
  • I can't reproduce the issue. Is that on Playground? If not, is there an error message in console when it crashes? Is it call in background? – Larme May 27 '21 at 17:08
  • Make sure to use it on the main thread. Note that you should not use it to display content downloaded asynchronously. **Discussion The HTML importer should not be called from a background thread (that is, the options dictionary includes documentType with a value of html). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs).** – Leo Dabus May 27 '21 at 17:37
  • **The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.** – Leo Dabus May 27 '21 at 17:38
  • https://stackoverflow.com/a/28132610/2303865 – Leo Dabus May 27 '21 at 17:40
  • Btw don't you have a better name for your property? – Leo Dabus May 27 '21 at 18:01
  • @LeoDabus This is called inside tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath). The data is retrieved from the API and then I reloadData(): // Reload tableview DispatchQueue.main.async { self.setupView() self.eventTableView.reloadData() } – Laith May 27 '21 at 22:31
  • @LeoDabus I agree about the property name. This was just a copy paste from an example I was trying. The example uses hard coded html. When i get it working I'll use my property. – Laith May 27 '21 at 22:32

0 Answers0