0

I am using a UITableView with custom Cell to render HTML code into an UITextView with AttributedString. The UITextView fit the cell and I am using Auto-Layout to get the dynamic height based on the content of the UITextView.

The html I use for the example is

<h1>hey this is a <a href="www.google.com">link</a><h1>

It seems that the height found is incorrect.

enter image description here

I also tried to determine the height of the content base on the string with

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let text = content[indexPath.row]
    let tv = UITextView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bouds.width - 16 * 2, height: 1.0))
    tv.isScrollEnabled = false
    tv.textContainerInset = UIEdgeInsets.zero
    tv.textContainer.lineFragmentPadding = 0
    let height = TextContentCell.calculateHeight(textView: tv, data: text).height
    return height
}

The calculateHeight function:

class func calculateHeight(textView:UITextView, data:String) -> CGRect {

    var newFrame:CGRect!
    do {
        let attr = try NSAttributedString(data: data.data(using: String.Encoding.utf8)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html,NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        textView.attributedText = attr
    } catch {
        //Handle Exceptions
    }

    let fixedWidth = textView.frame.size.width
    textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
    newFrame = textView.frame
    newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
    print("height \(newFrame.height)")
    return newFrame
}

This technique seems to return the exact same height that the dynamic height returned.

enter image description here

This problem occurs with html text no regular text.

enter image description here

Florian Ldt
  • 1,125
  • 3
  • 13
  • 31
  • you can try to remove all your `html tag` before calculate the height – Quoc Nguyen Aug 29 '18 at 03:46
  • Well a really good idea if this issue can’t be fixed in other way. Thank you for that – Florian Ldt Aug 29 '18 at 04:04
  • I think you can try to use WKWebView to load your html string, and then calculate height well. It supports the image, too. https://stackoverflow.com/questions/27515236/how-to-determine-the-content-size-of-a-wkwebview – Quoc Nguyen Aug 29 '18 at 04:19
  • @QuocNguyen WKWebView is too heavy for a task like this – Cristik Aug 29 '18 at 04:36
  • @QuocNguyen yes I first tried WKWebView but it seemed too heavy to use it in multiple UITableViewCell – Florian Ldt Aug 29 '18 at 04:39
  • @QuocNguyen for your idea to remove the html tag, if you have

    title

    that use a big font, if remove the html tags, you will have a smaller UITextView then the

    text height

    – Florian Ldt Aug 29 '18 at 04:50
  • @Florian_L How about convert html to NSAttributedString? https://stackoverflow.com/a/18886718/2776008 – Quoc Nguyen Aug 29 '18 at 05:29
  • @QuocNguyen I already use this system to transform the html to an AttributedString, the problem is the height of the UITextView doesn't match the height of the text – Florian Ldt Aug 29 '18 at 05:44
  • @Florian_L seem like i found your problem. When you using

    , it auto add 2 newline char (\n) to your string, so your string is too long.

    – Quoc Nguyen Aug 29 '18 at 06:31
  • Which iOS version are you using? – Aris Aug 29 '18 at 06:58
  • @Aris iOS 11.4 on simulator and 11.4+ on real device – Florian Ldt Aug 29 '18 at 07:01
  • @QuocNguyen yes I also saw it while setting the UITextView isEditable to true (saw only one new line here) – Florian Ldt Aug 29 '18 at 07:02
  • @QuocNguyen but didn't find a good solution to remove this space without loosing the style of the AttributedString – Florian Ldt Aug 29 '18 at 07:14
  • @Florian_L I think that this is the correct behaviour since

    tags have bottom margin by default. If you remove the h1 tags the height will be what you expect

    – Aris Aug 29 '18 at 07:31
  • @Florian_L do you have any solution to resolve it so far? – Phuc Dang Apr 09 '20 at 01:58

0 Answers0