0

I have this method that returns the height of NSString and returns the height on tableview method : heightForRowAtIndexPath but when the NSString gets longer than maybe 2 paragraphs a small section at the end of the string gets cut off.

- (float)heightWithFont:(UIFont *)font width:(float)width {    
CGRect frame = [string boundingRectWithSize:CGSizeMake(width, 9999.f)
                                  options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                               attributes:@{ NSFontAttributeName: font}
                                  context:nil];
return frame.size.height; }
julien
  • 437
  • 4
  • 9
  • Are you sure that the width is correct? 1 pixel could matter. – rmaddy Jun 25 '15 at 20:39
  • The width is based on the screen size. UIScreen.mainScreen.bounds.size.width - 16.f which is the baseHeight. – julien Jun 25 '15 at 20:43
  • Why isn't it based on the size of the table view? And where did the 16 come from? – rmaddy Jun 25 '15 at 20:44
  • 16 is hardcoded to give it extra spacing, Im not sure why its not based on the tableview (working on code that was written by someone else). But I tried to base it on the tableview and the result is the same. – julien Jun 25 '15 at 20:49
  • Base it on the table view an then figure out what the 16 needs to be changed to. – rmaddy Jun 25 '15 at 20:50
  • Changed the 16 to 40 and the string is no longer cut out but some strings had more/extra spaces at the end. – julien Jun 25 '15 at 21:02
  • See here https://stackoverflow.com/questions/12084760/nsstring-boundingrectwithsize-slightly-underestimating-the-correct-height-why/23563152#23563152 – Anton Tropashko Oct 18 '17 at 09:03

0 Answers0