Skip to content

Instantly share code, notes, and snippets.

@serpent7776
Last active Mar 18, 2018
Embed
What would you like to do?
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