Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last line of NSAttributedString not rendered in UILabel

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&#39;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>

screen capture with two labels. The top has an NSAttributedString, the bottom has a plain NSString.

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.

like image 535
Vale H Avatar asked Mar 26 '14 07:03

Vale H


1 Answers

Ok, I figured it out. It is not an obvious solution, but I did do a face-palm after discovering it.

The problem is that the p tag is being given a margin and/or padding, which is pushing the text outside of the bounds of the UILabel. I don't know why it isn't being taken into consideration when the size of the string is computed, but I don't care anymore.

The solution was for me to add p {margin:0; padding:0;} to the style tag that I was prepending to the HTML string. Now all of the string is drawn properly inside the UILabel.

like image 76
Vale H Avatar answered Sep 23 '22 06:09

Vale H