4

I am attempting to display, correctly formatted HTML text in a UILabel and have succeeded with the following code:

    // Create the html body
    var attributedHTMLBody = NSAttributedString(data: comment.bodyHTML.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil)

    // Create the string including the text style
    var htmlString = String(format: "<div style='font-size: 16px; font-family: HelveticaNeue-Light;'>%@", attributedHTMLBody.string)

    // Create the final text to display
    var attributedHML = NSAttributedString(data: htmlString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil)

    // Set the text
    cell.commentLabel.attributedText = attributedHML

    // Find the locations of any links
    var mutableLinkArray = NSMutableArray()
    var mutableRangeArray = NSMutableArray()
    attributedHML.enumerateAttributesInRange(NSMakeRange(0, attributedHML.length), options: NSAttributedStringEnumerationOptions.LongestEffectiveRangeNotRequired, usingBlock: {attribute, range, stop in

        // Get the attributes
        var attributeDictionary = NSDictionary(dictionary: attribute)

        if let link = attributeDictionary.objectForKey("NSLink") as? NSURL {
            mutableLinkArray.addObject(link)
            mutableRangeArray.addObject(range)
        }

    })

    // Add links to the label
    for var i = 0; i < mutableLinkArray.count; i++ {
        cell.commentLabel.addLinkToURL(mutableLinkArray[i] as NSURL, withRange: mutableRangeArray[i] as NSRange)
    }

    // Set the labels delegate
    cell.commentLabel.delegate = self

The code also correctly finds and locates the links in the text and allows the user to press on them by using the TTTAtributedLabel delegate.

This code however runs very slowly and the table cells do not allow for smooth scrolling, instead it stops completely then jumps down after the cells have been created.

Just as a note, I have tried commenting out the enumerating of the attributes to see if this is the problem but this does not speed up the cells creation at all.

How can I improve this code, thanks!

matthew.kempson
  • 1,014
  • 1
  • 12
  • 23
  • Try using a text view instead of a label and see if that helps. – Léo Natan Oct 08 '14 at 10:40
  • I have tried using a text view but if I could use a UILabel (TTTAtributedLabel) it would be much simpler for the calculations of the height of the cell etc.. – matthew.kempson Oct 08 '14 at 10:50
  • What OS are versions are you supporting? With iOS7 and above, calculating the required height for a text view is quite simple. With iOS8, you can let the system do it for you with autolayout. – Léo Natan Oct 08 '14 at 10:52
  • I'm aiming for iOS8. Could you provide me with a simple example project or code to show me thanks! – matthew.kempson Oct 08 '14 at 16:41

1 Answers1

6

Use UITextView.

With iOS8, achieving dynamic cell height is very simple. Set up a cell with the text view, and set up constraints around your text view and other views on the cell. Make sure to set the content compression resistance and content hugging priority to 100% in interface builder. You can achieve all this in code, but is much simpler in interface builder.

Now in code, when your controller loads, set the estimated cell height to an estimation you feel is somewhat accurate. The table view will display cells with the correct height according to content.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 1
    It is adding some bottom space by default at bottom when converting html string to attributed text to set in tableview.. what is the reason behind it – Mehul Thakkar Mar 10 '16 at 11:54