0

I need to render HTML which can have images, text on different cells of a UITableView. My datasource is of HTML content which I need to render on a UITableViewCell most probably.

I have also tried Attributed string on UILabel and UITextView but they also don't work great on scrolling the UITableView I tried this but could not get help with different types of cell.

I also tried https://github.com/Vugla/PSHTMLView, but this has issues like it helps in dequeueing similar cells only (I need to have cells with different html)

Any help will me much appreciated. Pointers will be also helpful :)

Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54

2 Answers2

1

I have faced similar use cases and was always a pain. So, I can think mainly of two options:

  1. Using the available native APIs.

You convert the HTML to an attributed string like this (see Convert HTML to NSAttributedString in iOS):

let htmlData = NSString(string: details).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType:
    NSAttributedString.DocumentType.html]
let attributedString = try? NSAttributedString(data: htmlData ?? Data(),
                                                      options: options,
                                                      documentAttributes: nil)

However, this is slow and when used in combination with UITableViewCell, may lead to irratic crashes. I suppose the reason is that the native HTML parser has to load WebKit and parse the HTML on a background thread or queue, which does not play well with UITableViewCell rendering pipe line. So, if you go down this path, I suggest to preprocess the HTML (and cache it) and let the UITableViewCell use the attributed strings as a data source.

  1. Use third party libs.

DTCoreText (https://github.com/Cocoanetics/DTCoreText) seems to solve it, but you should be aware of the licensing.

If you wish just to strip the HTML tags you could use this extension from Google https://chromium.googlesource.com/external/github.com/google/google-toolbox-for-mac/+/refs/heads/master/Foundation/GTMNSString+HTML.h

Ivan S Ivanov
  • 532
  • 5
  • 10
  • Ya solution 1 is a pain. I will try solution 2 thanks for that . Can you explain what licensing are we talking abt? – Ankit Kumar Gupta Dec 22 '18 at 13:56
  • I am not expert in licensing, but it seems that you have to mention the author in your app or pay 75 euros. Here is the full file: https://github.com/Cocoanetics/DTCoreText/blob/develop/LICENSE The 75 euro is mentioned in the read me. – Ivan S Ivanov Dec 22 '18 at 14:03
0

Add these constraints with heightAdd constarint to your can use UITextView just add one constraint i.e height constraint with grater than and equal to property and change the constant to 999 and use following code in cell.

self.heightUITextView.constant = self.newStoryTextView.contentSize.height/2

Here heightUITextView is my height constraint and newStoryTextView is my UITextView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! YourTableViewCell

            cell.heightUITextView.constant = self.newStoryTextView.contentSize.height/2
            cell.view.setNeedsLayout()
            return cell
    }

Your TableCell:

class YourTableViewCell : UITableViewCell {

    @IBOutlet weak var viewHeight: NSLayoutConstraint!

}
Mahesh Shahane
  • 489
  • 5
  • 16