0

I have tried many solutions provided in stackoverflow but I can't get the correct rect of a NSAttributedString.
As an instance the solution here does not work :

CGRect paragraphRect =
  [attributedText boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX)
  options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
  context:nil];

Below my code :

NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
paragrapStyle.alignment = NSTextAlignmentCenter;
paragrapStyle.lineHeightMultiple = 1.2;

NSMutableAttributedString *textMutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
[textMutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, [textMutableAttributedString.string length])];
[textMutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:114/255. green:132/255. blue:141/255. alpha:1] range:NSMakeRange(0,[textMutableAttributedString.string length])];
[textMutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, [textMutableAttributedString.string length])];

NSAttributedString *textAttributedString = [[NSAttributedString alloc] initWithAttributedString:textMutableAttributedString];

[text setAttributedText:textAttributedString];

CGRect textAttributedStringRect = [textAttributedString boundingRectWithSize:CGSizeMake(text.frame.size.width, CGFLOAT_MAX)
                                                                     options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                                     context:nil];

textAttributedStringRect computed value is origin=(x=0, y=0) size=(width=260.12695, height=33.120003) while text.frame.size.width original value is 260. The computed height is completely wrong...

I have read many answers on this specific problem and none of them solve it. I hope I will have someone who knows an accurate way to calculate the rect.

Thank you for your help

Community
  • 1
  • 1
gsempe
  • 5,371
  • 2
  • 25
  • 29
  • `floorf(textAttributedStringRect.size.width)`? – duci9y Dec 09 '13 at 17:08
  • To be sure I have replay the test with floorf. It's not a problem of CGFloat precision but rather that 33 is not even close of the correct height – gsempe Dec 09 '13 at 17:12
  • What is the height you expect to see? – duci9y Dec 09 '13 at 17:13
  • When I use the computed height to resize the UILabel the string is truncated. Height should be at least 20px taller – gsempe Dec 09 '13 at 17:18
  • Removing NSStringDrawingUsesFontLeading did the trick for me. – Dimillian Dec 09 '13 at 17:21
  • 2
    Using `UILabel`'s `sizeToFit` method would be sufficient to resize your `UILabel`. – duci9y Dec 09 '13 at 17:21
  • @Dimillian77 I tried to remove NSStringDrawingUsesFontLeading but textAttributedString give the exact same result – gsempe Dec 09 '13 at 17:25
  • @duci9y For UILabel it's ok but when I need to draw the NSAttributedString I need the correct rect – gsempe Dec 09 '13 at 17:30
  • Why would you need to draw the string? Won't a label work? – duci9y Dec 09 '13 at 17:32
  • I need to draw the NSAttributedString when I build UITableViewCell via drawRect – gsempe Dec 09 '13 at 17:33
  • You are over-complicating things. What you need to do can be simply done by using a UILabel. – duci9y Dec 09 '13 at 17:33
  • On complex view hierarchies drawing is still the fastest way to build them. Unless there is new benchmarks available that you know? – gsempe Dec 09 '13 at 17:37
  • Um… using a UILabel would simply mean you will not have to draw things on your own. UILabel would draw the text for you. And I'm not sure about your first statement. – duci9y Dec 09 '13 at 17:39
  • @duci9y When there is several subviews in a mother UIView there is timing penalty to draw them. To get the view hierarchy renders faster is common to draw the different elements and not use directly views – gsempe Dec 09 '13 at 17:44
  • You're wrong. The latency is absurdly minuscule to be noticeable. Unless you're using super-expensive drawing code in custom views. – duci9y Dec 09 '13 at 17:45
  • The problem was elsewhere. The solution [here][1] works [1]: http://stackoverflow.com/questions/13621084/boundingrectwithsize-for-nsattributedstring-returning-wrong-size – gsempe Dec 09 '13 at 18:55

0 Answers0