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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With