The problem is that UITextView does not support fill attributes inside HTML content. With this in mind, you have two options:
For one thing, use UIWebView as suggested in the answers. You will have the same functionality as in the UITextView , but you will need to calculate the height of the text using something like this:
This method returns the height excluding UITextView attachments, you will need to add them later.
func heightForText(text: String?, width: CGFloat) -> CGFloat { //Get attributed text from string let attributedText = text?.toHtmlTextWithAttributes() if let attributedText = attributedText { let rect = attributedText.boundingRectWithSize( CGSizeMake(width, CGFloat.max), options: [.UsesLineFragmentOrigin, .UsesFontLeading], context: nil) return rect.height } //Return any default height return 100 }
Here is a String extension that converts text to NSAttributedString .
extension String { public func toHtmlTextWithAttributes() -> NSAttributedString? { let encodedData = self.dataUsingEncoding(NSUTF8StringEncoding) if let data = encodedData { let paragrahpStyle = NSMutableParagraphStyle() paragrahpStyle.lineBreakMode = .ByWordWrapping let attributes: [String: NSObject] = [ NSParagraphStyleAttributeName: paragrahpStyle, NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding ] return try! NSAttributedString(data: data, options: attributes, documentAttributes: nil) } return nil } }
Also, the use of UITableViewAutomaticDimension not recommended, as in fairly large lists this can lead to performance problems.
On the other hand, if you need to use UITextView all ways, you can UITextView html, extract the heading and paragraph, and display them in two separate UITextView . This way you can customize the inserts of the second UITextView as follows:
textView.textContainerInset = UIEdgeInsetsMake(0, 20, 0, 0)