PDA

View Full Version : What DPI is used by NSString sizeWithFont/etc?




Thomas Harte
Nov 4, 2009, 04:39 PM
sizeWithFont returns the size of an NSString if you were to draw it in pixels using the font specified. I was wondering: does anybody know what DPI is assumed by this method, or how I can query the DPI at runtime?



Niiro13
Nov 4, 2009, 05:40 PM
The DPI is whatever the iPhone's resolution is within it's screen physical size. So the normal iPhone window is 320 x 480. If sizeWithFont returns CGSizeMake(320, 480) then the text fills the entire screen.

dejo
Nov 4, 2009, 05:54 PM
The DPI on the iPhone is constant: ~160 DPI.

Thomas Harte
Nov 4, 2009, 06:55 PM
I'm not drawing to the phone's screen, I'm drawing to a CGContext previously created with CGBitmapContextCreate. So it seems the DPI is set by the CGContext and, for now, you don't actually set that — it's just magically picked internally? I guess Apple's sample code in Texture2D initWithString is a good variant on the sort of situation I'm talking about

Experimentally, I'm getting something broadly close to 80dpi. The best docs I can find online is that Desktop OS X's CGBitmapContextCreate will always create contexts with 72dpi, which could actually be what I'm getting, for all I can tell.

So, I guess the corrected question is:

if I create a graphics context with CGBitmapContextCreate, what DPI will it have?

PhoneyDeveloper
Nov 4, 2009, 09:12 PM
Almost certainly the same as the screen.

I've done this. Make a bitmap that's the size of a string. Draw the string into it. Later draw the bitmap to a view. The text looked as expected.

Thomas Harte
Nov 5, 2009, 04:51 AM
If it's the screen then the results are very odd. For example, size 24 Helvetica is reported as 29 pixels high. If the reported height was in pixels, the thing was actually working in 160 dpi and Helvetica follows the usual convention that one em = the height of the font then size 24 Helvetica should be 24/72ths of an inch high, which should be 24/72 * 160 pixels, i.e. 53 pixels when rounded to the nearest integer.

As a result, I can say definitively that the reported height is not at 160dpi.

The output is eventually streamed to OpenGL and used for the presentation of a vector thing that uses proper typographic coordinates throughout. So I do really need this information.