iOS - Get the "real" height of a letter

shannoga picture shannoga · Jan 26, 2012 · Viewed 10.7k times · Source

I am trying to layout text on a UIView.

(The yellow area is the frame of the UILabel with a background color).

When I use sizeWithFont I get this, which has a very large space above the letter:

p with sizeWithFont

When I use font.pointSize i get this for "i" which is good-

p with font.pointSize

BUT When i use it for "p" I get the precise height but the letter is drawn in the bottom and cropped.

p with font.pointSize

**How can i get get the glyph only centered in the frame ? **

Thanks

Shani

Answer

Jeff Kelley picture Jeff Kelley · Jan 26, 2012

There are a lot of properties on UIFont to help in this situation:

  • pointSize
  • ascender
  • descender
  • capHeight
  • xHeight
  • lineHeight