Getting the physical screen size in inches for iPhone

有些话、适合烂在心里 提交于 2019-12-04 03:31:46

问题


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.


回答1:


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.




回答2:


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




回答3:


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)


来源:https://stackoverflow.com/questions/14517356/getting-the-physical-screen-size-in-inches-for-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!