Forum Xamarin.Forms

Screen density on iOS

symniksymnik Member ✭✭
edited February 18 in Xamarin.Forms

Hi,

I need to be able to set the height and width of some elements in inches and I'm trying to figure out the correct formula.
I have seen from other posts here that I can use DeviceDisplay.MainDisplayInfo.Density in order to calculate the pixels per inch but this property returns '2' for me when trying on a 9.7 inch iPad simulator and physical device.
According to the technical specifications of this iPad model it should have a density of 264 pixels per inch (ppi).
Any idea why I get a density value of 2?

Also, the documentation states that I can assume that 160dp is an inch and 64dp is a centimeter but this doesn't seem very exact. Is there a better way for me to be able to size views to be exactly a certain number of inches/cm/mm?

Thanks

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can use Xamarin.Essentials nuget to get the information about the device's screen metrics the application is running on.

    The following information is exposed through the API:

    // Get Metrics
    var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
    
    // Orientation (Landscape, Portrait, Square, Unknown)
    var orientation = mainDisplayInfo.Orientation;
    
    // Rotation (0, 90, 180, 270)
    var rotation = mainDisplayInfo.Rotation;
    
    // Width (in pixels)
    var width = mainDisplayInfo.Width;
    
    // Height (in pixels)
    var height = mainDisplayInfo.Height;
    
    // Screen density
    var density = mainDisplayInfo.Density;
    

    For more details, you can check:

    https://stackoverflow.com/questions/41489532/density-of-screen-in-ios-and-universal-windows-app
    https://docs.microsoft.com/en-us/xamarin/essentials/device-display?tabs=ios


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • symniksymnik Member ✭✭

    I know that, but what I don't understand is why the Density property returns the value 2. The iPad in question has a screen density of 264 ppi.

  • jezhjezh Member, Xamarin Team Xamurai

    We should be aware of the difference between screen density and screen pixel density.

    the Density property returns the value 2

    The coordinate system iOS uses to place content onscreen is based on measurements in points, which map to pixels in the display. A standard-resolution display has a 1:1 pixel density (or @1x), where one pixel is equal to one point. High-resolution displays have a higher pixel density, offering a scale factor of 2.0 or 3.0 (referred to as @2x and @3x). As a result, high-resolution displays demand images with more pixels.

    While the value 2 means that's what it means.

    And the screen pixel density refers to how many pixels have been squeezed into a physical amount of space (often an inch).

    You can check the following table:


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.