Measure the largest character in CGFont

I use CoreAnimation to create a presentation consisting of several layers that will be animated later. There is also one CATextLayerthat will contain only one character of a given font in a given size. To make this pretty, I would like to set the borders of the layer so that the character fits neatly.

The problem is that I have to determine the size of the largest character in CGFont, which will become the basis for calculating the borders for the text layer.

This SO question measures the size of each character in a string explains how to get the size of each character. Thus, one solution would be to iterate over the font characters and find out the largest character.

However, I found a function CGFontGetFontBBox. documentation says

Return value

Bounding box font.

Discussion

The bounding font is the union of all bounding fields for all glyphs in the> font. The value is in units of glyph space.

It seems to me that this is exactly what I want. One problem remains: I need to convert units of glyph space back to pixels. I tried it with the following code, but it gives strange results:

/* calculate the bounding box of the biggest character in the font with a given
 * font size
 */
- (CGSize) boundingBoxForFont:(CGFontRef)aFont withSize:(CGFloat)aSize
{
    if (!aFont) {
        CFStringRef fontName = CFStringCreateWithCString(NULL, "Helvetica", CFStringGetSystemEncoding());
        aFont = CGFontCreateWithFontName(fontName);
        CFRelease(fontName);
    }
    CGRect bbox = CGFontGetFontBBox(aFont);
    int units = CGFontGetUnitsPerEm(aFont);
    CGFloat maxHeight = ( CGRectGetHeight(bbox) / (CGFloat) units ) * aSize;
    CGFloat maxWidth = ( CGRectGetWidth(bbox) / (CGFloat) units ) * aSize;
    return CGSizeMake(maxWidth, maxHeight);
}

It is strange that CGRect is bboxwider than the above, which does not make any sense to me, since the characters in the font are usually higher than the wider, but maybe I am using it incorrectly.

Is there an alternative to using this feature?

EDIT

, ? , , ?

+3
1

, , . , , ffl, , , , . , em dash - , .

+2