繁体   English   中英

获取 iPhone 的物理屏幕尺寸(以英寸为单位)

[英]Getting the physical screen size in inches for iPhone

如何以英寸为单位以编程方式获取屏幕尺寸(例如 iPhone 4、3.5 英寸)。

我通过检测 iPhone/iPad model 找到了一种方法,但硬编码不是我想要的,所以我看起来不像那样。

我发现了一个名为'GBDeviceInfo'的好GitHub项目:

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
}

这是' GBDeviceInfo

这将找到设备的对角线屏幕尺寸:

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将包含屏幕的对角线大小(以英寸为单位)。

用于屏幕的Swift 4版本

    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://github.com/detroit-labs/IRLSize

您可以按以下方式获得以英寸为单位的屏幕尺寸

let screenHeight = UIDevice.current.physicalScreenHeight
let screenWidth = UIDevice.current.physicalScreenWidth

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM