views:

2950

answers:

3

On an iPhone how do I calculate the size of a character in pixels for a given point size?

+1  A: 

You can't reliably convert points to pixels as the ppi (points-per-inch) will change from monitor to monitor. Have a read;

http://hsivonen.iki.fi/units/

http://stackoverflow.com/questions/139655/how-to-convert-pixels-to-points-px-to-pt-in-net-c

That said, some people have put together a few reference tables and calculators that may get you started;

http://www.unitconversion.org/typography/postscript-points-to-pixels-x-conversion.html

http://sureshjain.wordpress.com/2007/07/06/53/

MatW
I'm pretty sure people aren't plugging monitors into their iPhones.
Mike Daniels
What if Apple change the resolution of the iPhone without changing the physical screen size? The PPI would change and so any point to pixel conversions made at the old PPI would be redundant.
MatW
MatW - I agree that is something to consider but in that event I will most likely have to retest my app anyway. I imagine quite a few apps out there would have issues.
Ian1971
also, I think the NSString UIKit additions mentioned would get around this problem.
Ian1971
Mike Daniels: It doesn't matter. Not all iPhone-OS devices have the same resolution; the iPod touch is slightly lower-res. Moreover, Apple could, someday, add the ability for applications to address a TV or monitor that the device is connected to through a dock.
Peter Hosey
+12  A: 

Point sizes are defined as 1/72 of an inch. That is, a 72-point font is approximately 1 inch from the lowest descent to the highest ascent. So the maximum height of a glyph in a 72pt font is about 1 inch.

Apple's iphone tech specs page claims that the iPhone currently has a resolution of 163 pixels per inch. So 72 points is 163 pixels, or about 2.2639 pixels per point. Just remember that every glyph varies in height and width, so this is a very rough estimate of size. Generally, the distance between baselines will be a bit larger than the font's point size so that lines of text don't crash into each other.

If you need exact measurements (and you probably do) then you'll need to actually measure the font glyphs using the font metric information. You can do this by using NSString's UIKit additions, which will let you measure the size of a particular string when rendered on screen.

Naaff
Also worth noting that the iPhone is not the only iPhone-OS device. The iPod touch is 160 dpi. So, already, not all iPhone-OS devices have the same resolution. If Apple ever doubles it, introduces a tablet, introduces a wristwatch, or whatever, and you're assuming fixed numbers of pixels, you're screwed. Be resolution independent, because if you don't, your app will someday break.
Peter Hosey
Hey, guess what - Apple introduced a tablet with a different dpi :-)
Phil Nash
+1  A: 

I believe you're looking for the UIFont NSString extensions that allow you to calculate the size of a string given a UIFont.

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

Specifically the sizeWithFont methods.

CynicismRising