NSString sizeWithFont:constrainedToSize:在视网膜显示器上返回不正确的高度

eri*_*ric 7 iphone sizewithfont iphone-4

我想我已经找到了sizeWithFont的一个边缘情况:constrainedToSize:在视网膜显示器上,它有时(似乎基于自动换行)返回高于实际需要的高度1行,更重要的是它实际绘制的高度.

注意:我正在使用的真实代码被隐藏在高性能的中心手绘可变高度表视图单元代码中,因此我将问题简化为尽可能简单的示例代码.(当我试图回答我的问题以外的其他问题时请注意这一点:-)

这个示例UIView填充它的内容,测量文本以适合(包装),填充矩形,然后绘制文本.

在视网膜设备(或模拟器)上,高度返回1线太高,但在视网膜前设备(或模拟器)上返回正确的高度.

我非常感谢任何人可能拥有的任何洞察力,因为这是我想修复的错误!

非常感谢!

-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];
}
Run Code Online (Sandbox Code Playgroud)

整个简单的项目,请点击这里.

模拟器图像:
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

Sum*_*Sum 2

看来是你模拟器的问题。这是我在 OS 4.3.2 上使用 Retina 模拟器运行它时得到的结果

在此输入图像描述