NSString sizeWithFont: constrainedToSize: returning the wrong height on retina displays

I think I found a marginal case for sizeWithFont: constrainedToSize: where on the retina screen it sometimes (apparently based on word wrap) returns a height 1 line higher than it really is needed, and more importantly than it really draws.

NOTE. The real code that I use is buried inside the standard table height table vector with vertical orientation, so I redid the problem to the simplest example code. (Please pay attention to this when trying to answer something other than my question :-)

This UIView sample fills it with content, measures the text in accordance (wrapped), fills this rectangle, then draws the text.

On a retina device (or simulator), the height returns 1 line too high, but on a device with a pre-grid (or simulator), it returns the correct height.

I would really appreciate any understanding anyone could have, as this is a mistake that I would like to correct!

Many thanks!

Eric

- (void)drawRect:(CGRect)rect { NSString * theString = @"Lorem ipsum dolor sit ameyyet, consectetur adipiscing elit. Etiam vel justo leo. Curabitur porta, elit vel."; UIFont * theFont = [UIFont systemFontOfSize:12]; CGSize theConstraint = CGSizeMake(rect.size.width - 20, rect.size.height - 20); CGSize theResultSize = [theString sizeWithFont:theFont constrainedToSize:theConstraint]; // dump the measurements NSLog(@"returned a size h = %f, w = %f", theResultSize.height, theResultSize.width); // fill the whole rect CGContextRef context = UIGraphicsGetCurrentContext(); [[UIColor yellowColor] set]; CGContextFillRect(context, rect); // fill the measured rect CGRect theRect = CGRectMake(10, 10, theResultSize.width, theResultSize.height); context = UIGraphicsGetCurrentContext(); [[UIColor cyanColor] set]; CGContextFillRect(context, theRect); // draw the text [[UIColor blackColor] set]; [theString drawInRect:theRect withFont:theFont]; } 

The whole simple project is available here .

Simulator Images:
http://files.droplr.com/files/9979822/aLDJ.Screen%20shot%202011-01-11%20at%2012%3A34%3A34.png http://files.droplr.com/files/9979822/YpCM .Screen% 20shot% 202011-01-11% 20at% 2012% 3A36% 3A47.png

+7
source share
3 answers

There seems to be a problem with your simulator. This is what I got when I launched it using the Retina simulator on OS 4.3.2.

enter image description here

+2
source

Below is the method that I use to determine the height of the label for dynamic text content. This works great in my application.

 - (float) getHeightFortheDynamicLabel: (NSString *) stringForTheLabel {   UITextView * aSampleTextView;   // 30 -     aSampleTextView = [[UITextView alloc] initWithFrame: CGRectMake (0, 0, mywidth, 30)];   aSampleTextView.text = stringForTheLabel;   aSampleTextView.font = [UIFont systemFontOfSize: kMyFontSize];   aSampleTextView.alpha = 0;   [self.view addSubview: aSampleTextView];   float textViewHeight = aSampleTextView.contentSize.height;   [aSampleTextView removeFromSuperview];   [aSampleTextView release];   return textViewHeight; 
}
- (float) getHeightFortheDynamicLabel: (NSString *) stringForTheLabel { UITextView * aSampleTextView; // 30 - aSampleTextView = [[UITextView alloc] initWithFrame: CGRectMake (0, 0, mywidth, 30)]; aSampleTextView.text = stringForTheLabel; aSampleTextView.font = [UIFont systemFontOfSize: kMyFontSize]; aSampleTextView.alpha = 0; [self.view addSubview: aSampleTextView]; float textViewHeight = aSampleTextView.contentSize.height; [aSampleTextView removeFromSuperview]; [aSampleTextView release]; return textViewHeight;
}
+1
source
  NSString *strSubstring = @"asdfghjklasdfghjkl adsds"; CGFloat maxWidth = 205.0; CGFloat maxHeight = 9999; CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight); CGSize expectedLabelSize = [strSubstring sizeWithFont:[UIFont fontWithName:@"StagSans-Light" size:12] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; NSLog(@"returned a size h = %f, w = %f", expectedLabelSize.height, expectedLabelSize.width); return expectedLabelSize; 
0
source

All Articles