Getting the physical screen size in inches for iPhone
Asked Answered
K

4

7

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.

Ku answered 25/1, 2013 at 7:32 Comment(3)
Why, for the love of Chtulhu, would you want that? Im just kidding, but this requirement is a sure sign that you're on the wrong track. And in inches, even...Malignant
@Malignant I cannot remember what it was called but there was a formula to calculate the most optimum video bit rate based on the screen size. So it was for that reason. Now looking back, I'm glad I didn't hard code or used a hard coded library like MCKapur suggested, as since then 5 more iPhones released and many more new iOS devices were released.Ku
@katzenhut: Ruler that displays an inch on the screen that is actually an inch long.Macleod
B
7

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.

Birdt answered 25/1, 2013 at 8:27 Comment(4)
Is this really giving the correct value for all devices ? That way we could findout the screensize of the device, but everyone is telling in forums this is not possible.Orola
Nope iOS devices have a wide range of screens with different PPI counts: 132, 264, 326, and 401. As far as I know you can't get the PPI from a device using the SDK. The only option I see is mapping each individual device name with the appropriate PPI.Syllogism
Use UIScreen nativeScale to get the scale factor for the physical screen. As result will be 401 PPI for a plus version or 458 for XPotts
This is wrong solution. The values are incorrect for some devices.Osterman
K
7

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)
Kinfolk answered 30/1, 2018 at 14:55 Comment(2)
This is wrong solution. e.g. iPhone 12 Pro Max is 6.7'', but the code returns 6.3''.Osterman
This won't work, because PPI is different for different devices...Cover
S
5

I found a nice GitHub project called 'GBDeviceInfo':

if ([GBDeviceInfo deviceDetails].display == GBDeviceDisplayiPhone35Inch) {
    //3.5 inch iphone
}
else if ([GBDeviceInfo deviceDetails].display == GBDeviceDisplayiPhone4Inch) {
    //4 inch iphone
}
else if ([GBDeviceInfo deviceDetails].display == GBDeviceDisplayiPad) {
    //ipad
}

Here 'tis: GBDeviceInfo

Sachikosachs answered 25/1, 2013 at 7:41 Comment(1)
This is no longer accurate, iPhone 6 plus reports a 5.2 inch screen which is actually 5.5 inches as well as a few other incorrect screen sizesMadder
A
0

I found this library, which can be used to get the screen size in inches

https://github.com/detroit-labs/IRLSize

You can get the screen size in inches as below

let screenHeight = UIDevice.current.physicalScreenHeight
let screenWidth = UIDevice.current.physicalScreenWidth
Algar answered 20/8, 2021 at 5:32 Comment(1)
Please don't just post some tool / package or library as an answer. At least demonstrate how it solves the problem in the answer itself.Vanpelt

© 2022 - 2024 — McMap. All rights reserved.