1

我现在正在使用模拟器将我的旧应用程序调整为 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 代码没有使用与渲染代码相同的逻辑。

由于更改错误文本是没有选择的:) 知道如何绕过此问题或解决它吗?

提前致谢

4

3 回答 3

0

我也遇到了同样的问题,字体一样。我还没有找到一种方法来预测它何时会发生,而且它似乎永远不会超过精确的一行。它发生在设备上,就像在模拟器上一样。它发生在所有 iOS 4.0、4.0.1 和 4.0.2 中。我还没有检查 4.1 模拟器。

最终,我通过手动记录它发生的位置并在这些位置减去一个行高来解决问题。当我们升级到 4.1 时,我们将检查这种行为是否持续存在。

于 2010-08-25T11:15:55.627 回答
0

我注意到了这个问题。还注意到,在使用 iPhone 4 时,包含在文本字符串中的空格被计算为 4 pts 宽,但是如果您自己计算空格的宽度,则它被计算为 5pts 宽....(使用 ArialMT, 14pt)

于 2011-01-18T18:29:53.063 回答
0

大约一年前向 Apple 提交了这个错误后,我收到了一封来自他们的电子邮件,说这个问题应该在 iOS5 beta 1 上得到解决

于 2011-07-25T13:06:41.327 回答