Skip to content

Instantly share code, notes, and snippets.

@serpent7776
Last active March 18, 2018 13:15
Show Gist options
  • Save serpent7776/208bb8dbc67c03c6e631e11b99ede343 to your computer and use it in GitHub Desktop.
Save serpent7776/208bb8dbc67c03c6e631e11b99ede343 to your computer and use it in GitHub Desktop.
iOS - Points to pixels and Pixels 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;
float pixelPerInch; // 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 points = px * pointsPerInch / pixelPerInch;
return points;
}
- (CGFloat)pointsToPixels:(CGFloat)points {
CGFloat pointsPerInch = 72.0; // see: http://en.wikipedia.org/wiki/Point%5Fsize#Current%5FDTP%5Fpoint%5Fsystem
CGFloat scale = 1;
float pixelPerInch; // 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 px = points / pointsPerInch * pixelPerInch;
return px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment