What is the relationship between a font Glyph Ascender and Descender in iOS?
Asked Answered
R

1

6

I'm working with fonts in a UILabel (iOS7) and have come across something I'm hoping someone can explain: What is the relationship between a fonts' Glyph, Ascender, and Descender?

From the documents I've read the Ascender is the portion of a font above the baseline, the Descender is the portion below (returned as negative). The combined absolute values should be the max Height of the font.

From Apple's documentation

For example an Ascender of 255 and a Descender of -64 would give a total height of 319. However the Glyph height is returned as 228.4

EDIT: Here's the Glyph code:

CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)(uiFont.fontName), uiFont.pointSize, NULL);
UniChar ch = [msgLabel.text characterAtIndex:0];
CGGlyph glyph;
if (CTFontGetGlyphsForCharacters (ctFont, &ch, &glyph, 1)) {
    CGRect bounds = CTFontGetBoundingRectsForGlyphs (ctFont, kCTFontOrientationDefault, &glyph, nil, 1);
    float glyphHeight = bounds.size.height;
}

And here's the Ascender/Descender code:

float adHeight = myLabel.font.ascender-myLabel.font.descender; //Descender is always a negative value

So why does the Glyph height returned from CTFontGetBoundingRectsForGlyphs not equal the Ascender plus Descender?

Refrigerant answered 25/3, 2014 at 22:15 Comment(2)
How are you getting glyph height? Maybe show some code?Weissberg
Ok - added some code...Refrigerant
W
1

Every glyph (letter shape) in the font is a different size and shape, right? The glyph for character 'A' is taller than the glyph for character 'a', the top of the 't' glyph is different again.

Font.ascender is the maximum value for all letter shapes (glyphs) and Font.descender is the minimum value.

Any particular font could easily have an extra tall glyph that meant the Font.ascender value had no relation to the dimensions of a string that didn't contain that character.

Weissberg answered 29/3, 2014 at 18:7 Comment(3)
duh! So the ...GetGlyphs... function returns the height of the single character '&ch' whereas '.ascender' or '.descender' is the max/min for the entire Font. Which means that the Glyph for 'T' should be the same as the ascender, but not the same for 'a' or 'j'. Since there is no single character that meets both the max height of the ascender and the min height of the descender, I could never get the Glyph value to match the ascender+descender or font height.Refrigerant
I could supply an array to ...GetGlyph... containing "Tj" which should return the same value as ascender+descender...just a thought.Refrigerant
yes ascender is max height. This is not likely to be the top of a 'T' - the font probably contains icons, logos, Chinese characters, etc. If you look at the definition of CTFontGetBoundingRectsForGlyphs it is intended that you pass in a c array of glyphs and get back a c array of bounding rects - so your though is a good one.Weissberg

© 2022 - 2024 — McMap. All rights reserved.