Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the physical screen size in inches for iPhone

How can I get the screen size programmatically in inches(for example iPhone 4, 3.5 inches).

I found a way to do it by detecting the iPhone/iPad model but hard coding is not what I want so I am not looking something like that.

like image 217
Sarp Kaya Avatar asked Jan 25 '13 07:01

Sarp Kaya


2 Answers

Swift 4 Version for screen

    let scale = UIScreen.main.scale

    let ppi = scale * ((UIDevice.current.userInterfaceIdiom == .pad) ? 132 : 163);

    let width = UIScreen.main.bounds.size.width * scale
    let height = UIScreen.main.bounds.size.height * scale

    let horizontal = width / ppi, vertical = height / ppi;

    let diagonal = sqrt(pow(horizontal, 2) + pow(vertical, 2))
    let screenSize = String(format: "%0.1f", diagonal)
like image 90
Hitesh Agarwal Avatar answered Sep 22 '22 05:09

Hitesh Agarwal


This will find the diagonal screen size of a device:

float scale = [[UIScreen mainScreen] scale];

float ppi = scale * ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 132 : 163);

float width = ([[UIScreen mainScreen] bounds].size.width * scale);
float height = ([[UIScreen mainScreen] bounds].size.height * scale);

float horizontal = width / ppi, vertical = height / ppi;

float diagonal = sqrt(pow(horizontal, 2) + pow(vertical, 2));

diagonal will contain the diagonal size, in inches, of the screen.

like image 21
Jack Greenhill Avatar answered Sep 20 '22 05:09

Jack Greenhill