我现在正在使用模拟器将我的旧应用程序调整为 iPhone 4,我可以通过 UILabel 绘图和 sizeWithFont:constrainedToSize: 遇到一个非常奇怪的行为,我目前只能在 iPhone 4 模拟器上看到。
我试图向用户显示以下错误文本:@“不正确的用户名或密码”此文本位于由三个部分组成的动态错误框内:顶部、中心和底部,因此我计算标签的大小所以我可以相应地更改中心背景图像框架。
下面是一个 UILabel 大小计算代码的例子:
CGRect errorFrame = CGRectMake(40, 0, 240.0, 22.0);
UILabel *errorlabel = [[UILabel alloc] initWithFrame:errorFrame];
errorlabel.adjustsFontSizeToFitWidth = NO;
errorlabel.font = [UIFont fontWithName:@"HelveticaNeue" size:16];
errorlabel.textAlignment = UITextAlignmentLeft;
errorlabel.numberOfLines = 0;
errorlabel.text = @"Incorrect user name or password";
// since only the width is fixed I will use a really large height value
CGSize errorLabelSize = [errorlabel.text sizeWithFont:errorlabel.font constrainedToSize:CGSizeMake(240.0, 4600.0)];
CGRect newFrame = errorlabel.frame;
newFrame.size.height = errorLabelSize.height;
errorlabel.frame = newFrame;
// added so I can easily see the new frame
errorlabel.backgroundColor = [UIColor redColor];
[self.errorView addSubview:errorlabel];
[errorlabel release];
当我在 iPhone 3 模拟器上运行代码时,sizeWithFont:constrainedToSize: 方法返回 1 行的高度并在 1 行上绘制此错误文本。当我在 iPhone 4 模拟器上运行相同的代码时,sizeWithFont:constrainedToSize: 返回两行所需的 (170.0, 42.0) 大小,但标签本身绘制在 1 行上。就好像 sizeWithFont 代码没有使用与渲染代码相同的逻辑。
由于更改错误文本是没有选择的:) 知道如何绕过此问题或解决它吗?
提前致谢