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.
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.
This problem occurs with html text no regular text.



 
    
title
that use a big font, if remove the html tags, you will have a smaller UITextView then thetext height
– Florian Ldt Aug 29 '18 at 04:50, it auto add 2 newline char (\n) to your string, so your string is too long.
– Quoc Nguyen Aug 29 '18 at 06:31tags 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