Created
July 24, 2012 08:25
-
-
Save jordiboehme/3168819 to your computer and use it in GitHub Desktop.
iOS Pixel-to-Points conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(CGFloat)pixelToPoints:(CGFloat)px { | |
CGFloat pointsPerInch = 72.0; // see: http://en.wikipedia.org/wiki/Point%5Fsize#Current%5FDTP%5Fpoint%5Fsystem | |
CGFloat scale = 1; // We dont't use [[UIScreen mainScreen] scale] as we don't want the native pixel, we want pixels for UIFont - it does the retina scaling for us | |
float pixelPerInch; // aka dpi | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { | |
pixelPerInch = 132 * scale; | |
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { | |
pixelPerInch = 163 * scale; | |
} else { | |
pixelPerInch = 160 * scale; | |
} | |
CGFloat result = px * pointsPerInch / pixelPerInch; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
genial! muy buen codigo!