簡體   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