I can´t get the height for attributed string when I apply a paragraph style

Hello I have the next code:


NSMutableAttributedString *mutableText = [[NSMutableAttributedString alloc] initWithData:[Texto dataUsingEncoding:NSUTF8StringEncoding]

options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,

NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}

documentAttributes:nil

error:nil];


NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];

style.lineBreakMode = NSLineBreakByTruncatingTail;

[mutableText addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, mutableText.length)];


self.Texto.attributedText = mutableText;


[self.Texto setNeedsLayout];

[self.Texto layoutIfNeeded];


CGRect rect = [self.Texto.attributedText boundingRectWithSize:CGSizeMake(self.Texto.frame.size.width , NSUIntegerMax)

options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)

context:nil];


NSLog(@"%@ %@",NSStringFromCGRect(self.Texto.bounds),NSStringFromCGRect(rect));


This code is not working when I add the attribute NSMutableParagraphStyle to the NSMutableAttributedString. I´m getting 35 pixel for Height when I should get a 705 pixels for my text. I get the right value (704.8897) If I remove the next line:


[mutableText addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, mutableText.length)];


I´m lost, do you know where I´m wrong?

Replies

NSLineBreakByTruncatingTail is specifically meant to put it all on one line and truncate any text that doesn't fit, isn't it? You probably want some line break mode that will wrap to multiple lines.

According to documentation is valid for multiline:


"The line is displayed so that the beginning fits in the container and the missing text at the end of the line is indicated by an ellipsis glyph. Although this mode works for multiline text, it is more often used for single line text."


Visually at least is working.