Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get physical Size or scale factor #328

Open
alamyudi opened this issue Sep 17, 2022 · 2 comments
Open

How to get physical Size or scale factor #328

alamyudi opened this issue Sep 17, 2022 · 2 comments

Comments

@alamyudi
Copy link

Hi, very amazing library!

As far as I tried the screenRatio returns the device/simulator logical size. I am wondering is there a way to get the physical size or scale factor of the device/simulator?

@Zandor300
Copy link
Member

@alamyudi Currently, there is no way in DeviceKit to get the physical size of the device/screen. This could be a really nice addition to the library. Will look into it when I have time.

@mttlmnt
Copy link

mttlmnt commented May 1, 2023

Unless I'm misunderstanding, the physical screen size can be derived using the ppi feature of DeviceKit like so:

UIScreen.main.nativeBounds.width / Device.current.ppi

Or more generally:

    static var screenSize: (width: Measurement<UnitLength>, height: Measurement<UnitLength>) {
        let ppi = Device.current.ppi!
        var width = UIScreen.main.nativeBounds.width / Double(ppi)
        var height = UIScreen.main.nativeBounds.height / Double(ppi)

        if UIApplication.shared.interfaceOrientation?.isLandscape == true {
            // Swap the values, as `nativeBounds` does not change with the screen rotation
            swap(&width, &height)
        }

        return (
            width: Measurement(value: width, unit: .inches),
            height: Measurement(value: height, unit: .inches)
        )
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants