Last active
March 18, 2018 13:15
-
-
Save serpent7776/208bb8dbc67c03c6e631e11b99ede343 to your computer and use it in GitHub Desktop.
iOS - Points to pixels and Pixels to points conversion
This file contains 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; | |
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