目前,我正在使用模拟器将旧应用程序调整为iPhone 4,并且可以使用Uilabel Drawing和SizeWithFont:ConstrationTosize:我目前仅在iPhone 4 Simulator上看到的。

我试图向用户显示以下错误文本: @“不正确的用户名或密码”此文本位于一个动态错误框中,该框是由三个部分构建的:顶部,中心和底部,因此我计算了标签的大小因此,我可以相应地更改中心背景图像框架。

这是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:CondentAsedTosize:方法返回1行的高度,并在1行上绘制此错误文本。当我在iPhone 4 Simulator SizeWithFont上运行相同的代码时:CondentAsedTosize:返回大小为(170.0,42.0),这是两行所需的,但标签本身在1行上绘制。好像SizeWithFont代码不使用渲染代码的相同逻辑。

由于更改错误文本是没有选项的:)任何想法如何绕过此问题或解决该问题?

提前致谢

有帮助吗?

解决方案

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

最终,我通过手动指出发生的位置并在这些位置减去一条线高的地方进行了工作。当我们升级到4.1时,我们将检查该行为是否持续。

其他提示

我注意到了这个问题。还注意到,当使用iPhone 4时,将包含在一串文本中的空白空间计算为4 pts宽,但是如果您计算出空白空间的宽度,则将其计算为5点宽。使用Arialmt,14pt)

大约一年前向苹果提交此错误后,我收到他们的电子邮件,说该问题应在ios5 beta 1上解决

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top