iOS get physical screen size programmatically?

前端 未结 11 1926
情话喂你
情话喂你 2020-12-15 03:11

Is this possible? I want the number of inches, not the number of pixels. I know it is approximately 160 ppi. But not exactly.

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 03:50

    Here's a short method that estimates the device screen size. It's updated as to the latest devices, but may fail on future ones (as all methods of guessing might). It will also get confused if the device is being mirrored (returns the device's screen size, not the mirrored screen size)

    #define SCREEN_SIZE_IPHONE_CLASSIC 3.5
    #define SCREEN_SIZE_IPHONE_TALL 4.0
    #define SCREEN_SIZE_IPAD_CLASSIC 9.7
    
    + (CGFloat)screenPhysicalSize
    {
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            CGSize result = [[UIScreen mainScreen] bounds].size;
            if (result.height < 500)
                return SCREEN_SIZE_IPHONE_CLASSIC;  // iPhone 4S / 4th Gen iPod Touch or earlier
            else
                return SCREEN_SIZE_IPHONE_TALL;  // iPhone 5
        }
        else
        {
            return SCREEN_SIZE_IPAD_CLASSIC; // iPad
        }
    } 
    

提交回复
热议问题