如何使用 UIKit 计算文本的准确高度?

发布于 2024-12-25 07:27:08 字数 476 浏览 3 评论 0原文

我使用 -[NSString sizeWithFont] 来获取文本高度。字符“t”显然比“m”高,但 -sizeWithFont 为这两个字符串返回相同的高度。代码如下:

UIFont* myFont = [UIFont fontWithName:@"Helvetica" size:1000.0];
NSString* myStr = @"m";
CGSize mySize = [myStr sizeWithFont:myFont];

如图所示,使用“m”时,它返回 {834, 1151}。如果使用 myStr = @"t",则为 {278, 1151}。较小的宽度按预期显示,但高度不显示。

是否有其他函数将文本紧紧包裹?我理想地寻找类似于 Android 的 Paint.getTextBounds() 的东西。

I'm using -[NSString sizeWithFont] to get the text height. The character 't' is clearly taller than 'm', but -sizeWithFont returns the same height for both these strings. Here's the code:

UIFont* myFont = [UIFont fontWithName:@"Helvetica" size:1000.0];
NSString* myStr = @"m";
CGSize mySize = [myStr sizeWithFont:myFont];

With 'm' as shown, it returns {834, 1151}. With myStr = @"t" instead, it's {278, 1151}. The smaller width shows up as expected, but not the height.

Does some other function wrap the text tightly? I'm ideally looking for something equivalent to Android's Paint.getTextBounds().

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

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

发布评论

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

评论(2

筑梦 2025-01-01 07:27:08

您从此方法返回的信息基本上是字体的行高(即,它是您选择的字体的上升加上下降)。它不是基于单个字符,而是基于特定的字体规格。您可以从UIFont 类中获取有关字体规格的大部分信息(例如,方法-ascender 为您提供字体上升部分的高度)。大多数情况下,您将处理绘制带有该字体的高度上升部分和最低下降部分的字形所需的垂直空间总量。无法从 UIFont 获取有关各个字形的信息。如果您需要此信息,则必须查看 CoreText 框架,它为您提供了绘制和排列字形的更多灵活性,但使用起来要复杂得多。

有关在应用程序中处理文本的更多信息,请参阅 绘制和管理文本 部分的 文本、Web 和编辑编程指南。对于您需要处理的大多数框架和类来说,无论您采用 UIKit 还是 CoreText 路线,它也是一个很好的启动点。

The information you get back from this method is basically the line height for the font (i.e., it's the ascent plus the descent for the font you've chosen). It's not based on individual characters, it's based on specific font metrics. You can get most of the information about a font's metrics from the UIFont class (e.g., the method -ascender gives you the height of the ascender for the font). Mostly, you will be dealing with the total amount of vertical space needed to draw the glyphs with the heights ascenders and the lowest descenders for that font. There is no way to get information about individual glyphs from UIFont. If you need this information, you'll have to look at the CoreText framework, which gives you a lot more flexibility in how you draw and arrange glyphs but is far, far more complicated to use.

For more information on dealing with text in your app, please se the Drawing and Managing Text section of the Text, Web, and Editing Programming Guide. It is also a good launching point for most of the frameworks and classes you'll need to deal with whether you go the UIKit or the CoreText route.

佞臣 2025-01-01 07:27:08

嗯……我想你接下来只会处理单个角色。如上所述,sizeWithFont 会返回该字体最大字符的高度,因为无论您做什么,文本字段都将是该高度。您将能够获得最大的高度值(UIFont 的 CGFloat capHeight ),但是看起来您将处理各种类型的文本,

我将看看 CoreText 框架。启动 这里并继续阅读。不可避免地,你最终会得到类似这样的结果:

CGFloat GetLineHeightForFont(CTFontRef iFont)
{
    CGFloat lineHeight = 0.0;

    check(iFont != NULL);
    lineHeight += CTFontGetLeading(iFont);
    return lineHeight;
}

Hmmm... I assume you're only going to be working with individual characters then. sizeWithFont, as noted above, returns the height of the largest character of that font as the text field is going to be that height no matter what you do. You would be able to get the LARGEST height values (UIFont's CGFloat capHeight) however it looks like you're going to be working with all kinds of text

I would take a look at the CoreText framework. Start here and read away. Inevitably you're going to end up with something along the lines of this:

CGFloat GetLineHeightForFont(CTFontRef iFont)
{
    CGFloat lineHeight = 0.0;

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