Saturday, June 06, 2009

iPhone dev Stupidity 14: UIFont size

To understand the properties of UIFont (from Microsoft doc on opentype ):


 



Log info for a 42 pointsized system font:


> font size 21 50

> Font Helvetica properties: leading 50.000000, ascender 39.340820, capheight 30.534000, descender -9.659180, pointSize 42.000000


Leading: the height of the line.


To get the tight bounding box of a character, height = ascender.


And to center a char at the center of a view:


 


- (CGRect) centerRectForChar: (NSString*) txt{
 CGSize font_size = [text_ sizeWithFontfont_];
 int x = (VIEW_WIDTH/2) - (font_size.width/2);
 int y = (VIEW_HEIGHT/2) - (font_size.height/2);
 int w = font_size.width;
 int h = font_.ascender;

 return CGRectMake(x, y, w, h);
}

No comments: