What DPI is used by NSString sizeWithFont/etc?

Discussion in 'iOS Programming' started by Thomas Harte, Nov 4, 2009.

  1. macrumors 6502

    Joined:
    Nov 30, 2005
    #1
    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?
     
  2. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #2
    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.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  4. thread starter macrumors 6502

    Joined:
    Nov 30, 2005
    #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?
     
  5. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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.
     
  6. thread starter macrumors 6502

    Joined:
    Nov 30, 2005
    #6
    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.
     

Share This Page