Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Thomas Harte

macrumors 6502
Original poster
Nov 30, 2005
400
4
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

macrumors 68000
Feb 12, 2008
1,719
0
Illinois
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.
 

Thomas Harte

macrumors 6502
Original poster
Nov 30, 2005
400
4
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

macrumors 68040
Sep 2, 2008
3,114
93
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

macrumors 6502
Original poster
Nov 30, 2005
400
4
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.