Last line of NSAttributedString not displayed in UILabel

I have text that contains HTML formatting that I am trying to display in a view. I am currently using NSAtributedString to display text inside a UILabel .

I get the assigned 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 the label attributedText property. The self.descriptionText property is an NSString that can contain simple HTML markup, such as p tags or span tags with inline styles. The wrapping technique in an HTML document was found in another StackOverflow article.

UILabel set to Lines = 0 and Line Breaks = Word Wrap. The behavior I see is that the label grows properly to fit the entire line, but the last line of the line is never displayed, no matter how long the line lasts.

Here is a screenshot with two shortcuts. The top label (with a yellow background) has my HTML bound string. You can see that the label is sized to allow 3 lines of text, but only two are displayed, and the rest are truncated. The second label (with a red background) had a simple string assigned to its Text property, and it draws 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.

A valid answer explains how I can prevent the truncation of the last line or provide a simple alternative method for displaying HTML text in a view. Thanks.

+8
html ios objective-c uilabel nsattributedstring
source share
3 answers

Ok, I figured it out. This is not an obvious solution, but after that I made a palm.

The problem is that the p tag is assigned a margin and / or padding that clicks the text outside the borders of the UILabel . I don’t know why this is not taken into account when calculating the row size, but I don’t care.

The solution was to add p {margin:0; padding:0;} p {margin:0; padding:0;} to the style tag that I added to the HTML line. Now the entire line is drawn correctly inside the UILabel.

+10
source share

Just stumbled upon this. UILabel seems to have some difficulty calculating the correct size when showing attributedText , which has the NSParagraphStyleAttributeName attribute.

I solved the problem by removing this attribute from my NSMutableAttributedString before assigning it to my shortcut as follows:

 [mutableAttributedString removeAttribute:NSParagraphStyleAttributeName range:NSMakeRange(0, mutableAttributedString.length)]; 

The effect is the same, but it feels a little neater than assuming some inline CSS.

+5
source share

I had the same issue with the new iOS 11.

My solution was to insert a css string-height. Although this does not affect the displayed row height, it solved the problem and now all rows are displayed πŸ€”

body {font-size:14px;line-height:14px;}p {margin:0;padding:0;line-height:14px;}

0
source share

All Articles