iPhone 上的 UILabel 尺寸计算错误

发布于 2024-09-15 14:37:39 字数 1365 浏览 14 评论 0原文

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

由于无法更改错误文本:) 知道如何绕过此问题或解决它吗?

提前致谢

I am adjusting my old apps to iPhone 4 using the simulator at the moment and I can across a very strange behavior with UILabel drawing and sizeWithFont:constrainedToSize: that I currently see only on the iPhone 4 simulator.

I am trying to show the following error text to the user:
@"Incorrect user name or password"
This text sits inside a dynamic error box that is built from three parts: top, center and bottom and therefore I calculate the size of the label so I can change the center background image frame accordingly.

Here is an example of the UILabel size calculation code:

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];

When I run the code on the iPhone 3 simulator the sizeWithFont:constrainedToSize: method returns a height of 1 line and draws this error text on 1 line.
When I run the same code on the iPhone 4 simulator sizeWithFont:constrainedToSize: returns a size of (170.0, 42.0) which is needed for two lines but label itself is drawn on 1 line.
It is as if the sizeWithFont code doesn't use the same logics of the rendering code.

Since changing the error text is no option :) any idea how to bypass this issue or resolve it?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-09-22 14:37:42

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

After submitting this bug to Apple about a year ago I got an email from them saying the problem should be resolved on iOS5 beta 1

っ左 2024-09-22 14:37:41

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

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

I've got this same problem, with the same font. I haven't found a way to predict when it will occur, and it appears to never be more than precisely one line extra. It happens on the device exactly as on the simulator. It happens in all of iOS 4.0, 4.0.1, and 4.0.2. I haven't checked on the 4.1 simulator yet.

Eventually I worked around by manually noting the places it occurred and subtracting one line height in these locations. When we upgrade to 4.1 we'll check to see if this behaviour has persisted.

千鲤 2024-09-22 14:37:41

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

I noticed this issue. Also noticed that when using iPhone 4, a blank space that is included in a string of text is calculated as 4 pts wide, but if you calculate the width of a blank space by itself, it is calculated as 5pts wide.... (using ArialMT, 14pt)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文