NSString sizeWithFont:返回不一致的结果?已知的错误?

发布于 2024-09-03 06:54:22 字数 1009 浏览 0 评论 0原文

我正在尝试创建一个简单的自定义 UIView,其中包含用单一字体绘制的字符串,但第一个字符稍大。

我认为这可以通过两个相邻放置的 UILabel:s 轻松实现。

我使用 NSString sizeWithFont 来测量我的字符串,以便能够正确地布局它。

但我注意到返回的矩形中的字体基线根据我设置的字体大小而变化 +/- 1 像素。 这是我的代码:

NSString* ctxt = [text substringToIndex:1];
NSString* ttxt = [text substringFromIndex:1];

CGSize sz = [ctxt sizeWithFont: cfont ];
clbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sz.width, sz.height)];
clbl.text = ctxt;
clbl.font = cfont;
clbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:clbl];

CGSize sz2 = [ttxt sizeWithFont: tfont];
tlbl = [[UILabel alloc] initWithFrame:CGRectMake(sz.width, (sz.height - sz2.height), sz2.width, sz2.height)];
tlbl.text = ttxt;
tlbl.font = tfont;
tlbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:tlbl];

如果我使用 12.0 和 14.0 作为大小,它工作正常。 但如果我改用 13.0 和 15.0,那么第一个字符就高了 1 个像素。

这是一个已知问题吗?

有什么建议如何解决这个问题吗?

使用 CSS 和 HTML 页面创建 UIWebView 似乎有点矫枉过正。以及处理动态字符串的更多工作。这是我应该做的吗?

I'm trying to create a simple custom UIView wich contain a string drawn with a single font, but where the first character is slightly larger.

I thought this would be easily implemented with two UILabel:s placed next to eachother.

I use NSString sizeWithFont to measure my string to be able to lay it out correctly.

But I noticed that the font baseline in the returned rectangle varies with +/- 1 pixel depending on the font size I set.
Here is my code:

NSString* ctxt = [text substringToIndex:1];
NSString* ttxt = [text substringFromIndex:1];

CGSize sz = [ctxt sizeWithFont: cfont ];
clbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, sz.width, sz.height)];
clbl.text = ctxt;
clbl.font = cfont;
clbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:clbl];

CGSize sz2 = [ttxt sizeWithFont: tfont];
tlbl = [[UILabel alloc] initWithFrame:CGRectMake(sz.width, (sz.height - sz2.height), sz2.width, sz2.height)];
tlbl.text = ttxt;
tlbl.font = tfont;
tlbl.backgroundColor = [UIColor clearColor];
[contentView addSubview:tlbl];

If I use 12.0 and 14.0 as sizes, it works fine.
But if I instead use 13.0 and 15.0, then the first character is 1 pixel too high.

Is this a known problem?

Any suggestions how to work around it?

Creating a UIWebView with a CSS and HTML page seems way overkill for this. and more work to handle dynamic strings. Is that what I'm expected to do?

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

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

发布评论

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

评论(1

没︽人懂的悲伤 2024-09-10 06:54:22

找到答案...

当然,我还必须检查字体的下降值,并在布局中进行补偿。

第二个标签的新矩形是:
CGRectMake(sz.width, (sz.height - sz2.height) + Floor(cfont.descender - tfont.descender), sz2.width, sz2.height)

Floor() 是为了确保它捕捉到像素位置,或者字体会看起来模糊

Found the answer...

Ofcourse, I also have to check the descender value on the font, and compensate for that in the layout.

New rect for the second label is:
CGRectMake(sz.width, (sz.height - sz2.height) + floor(cfont.descender - tfont.descender), sz2.width, sz2.height)

floor() is to make sure it snaps to pixel position, or the font will look blurry

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