Skip to content

Instantly share code, notes, and snippets.

@jordiboehme
Created July 24, 2012 08:25
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jordiboehme/3168819 to your computer and use it in GitHub Desktop.
Save jordiboehme/3168819 to your computer and use it in GitHub Desktop.
iOS Pixel-to-Points conversion
+(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;
}
@erolando
Copy link

genial! muy buen codigo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment