Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 878
wayneh Avatar asked Nov 11 '22 10:11

wayneh


1 Answers

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.

like image 169
hooleyhoop Avatar answered Nov 15 '22 06:11

hooleyhoop