I have text which contains HTML formatting that I am trying to render on a view. Currently I am using an NSAtributedString
to display the text inside a UILabel
.
I obtain the attributed string as follows:
NSString *htmlString = @"<html>"
" <head>"
" <style type='text/css'>"
" body { font: 12pt 'Helvetica'; color: #111111; }"
" </style>"
" </head>"
" <body>";
htmlString = [htmlString stringByAppendingString:self.descriptionText];
htmlString = [htmlString stringByAppendingString:@"</body></html>"];
NSError *err = nil;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUTF8StringEncoding]
options: @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
documentAttributes: nil
error: &err];
and then assign it to my label's attributedText
property. The property self.descriptionText is an NSString
which can contain simple HTML markup such as p
tags or span
tags with inline styles. The technique of wrapping the string in an HTML document was found on another StackOverflow post.
The UILabel
is set to Lines = 0 and Line Breaks = Word Wrap. The behavior that I am seeing is that the label grows properly to accommodate the whole string, but the last line of the string is never rendered, no matter how long the string is.
Here is a screen capture with two labels. The top label (with a yellow background) has my HTML attributed string in it. You can see that the label has sized itself to allow 3 rows of text, but only two are rendered and the rest is truncated. The second label (with a red background) has had a plain string assigned to its Text property and it draws just fine.
The HTML string used in the example image contains the following text:
<p>With the world's longest fan-vaulted ceiling, an original Reubens painting, and an excellent collection of medieval stained glass, this chapel has intrigue from multiple angles</p>
An acceptable answer would explain how I can prevent the last line from being truncated or provide a simple alternative method for rendering HTML text to a view. Thanks.