PixelRatio: Bridge Layout Units to Physical Pixels
PixelRatio is your app's translator, converting abstract layout units into physical pixels and respecting the user's chosen font size. Use it to fetch correct image sizes and make fonts accessible. Ignoring getFontScale() creates a fixed, inaccessible UI.
Why it exists
Modern devices have vastly different screen sizes and pixel densities. A 100x100 pixel square looks huge on an old monitor but tiny on a 4K phone screen. Furthermore, users expect apps to respect their system-wide accessibility settings for text size. PixelRatio exists to bridge the gap between your abstract layout code and the physical reality of a user's device and preferences.
The mental model
Think of PixelRatio as a translator. Your app's layout speaks in an abstract language called 'density-independent pixels' (dp). PixelRatio translates this abstract language into two concrete outputs: the number of physical screen pixels required (get()) and the font size multiplier the user has requested (getFontScale()). It lets your UI adapt correctly to any environment.
How it works
PixelRatio provides several static methods to get device-specific information.
PixelRatio.get() returns the device's pixel density. A value of 2 means it's a '@2x' or 'retina' display, where one layout point corresponds to a 2x2 grid of physical pixels. A value of 3 means '@3x'.
PixelRatio.getFontScale() returns the user's preferred font size multiplier from the OS accessibility settings. If a user sets their system font to be larger, this value might be 1.2 or higher. Your app should multiply its base font sizes by this factor.
PixelRatio.getPixelSizeForLayoutSize(size) is a helper that converts a layout size in dp to a physical pixel size, effectively doing size * PixelRatio.get() and rounding to the nearest integer.
When to use it
Use PixelRatio when fetching images to request a higher-resolution asset for a high-density screen. For example, for a 100x100dp image view on a '@3x' device, you should request a 300x300px image to avoid blurriness. Most importantly, use it to calculate font sizes, line heights, and padding around text to ensure your UI is accessible and respects user settings.
When not to use it
Avoid scaling elements that are meant to be a fixed, absolute size. A 1dp hairline border should almost always render as the thinnest possible line on the device, regardless of font scaling. Applying font scale to everything can break layouts that rely on fixed-size icons or separators. Be intentional about what scales with text and what remains static.
One canonical example
To implement dynamic font scaling that respects user accessibility settings, you multiply your base font size by the font scale factor. This ensures your text is readable for all users.
import { PixelRatio, StyleSheet } from 'react-native';const baseFontSize = 16;const styles = StyleSheet.create({title: {
fontSize: baseFontSize * PixelRatio.getFontScale(),
},
body: {
fontSize: (baseFontSize 0.8) PixelRatio.getFontScale(),
}
});
Interview question
What primary accessibility concern does PixelRatio.getFontScale() address in a React Native application?
- a.Preventing UI components from overlapping or breaking layout on extremely small or large screens.
- b.Adapting the application's text sizes to match the user's system-wide font preferences.Correct
- c.Ensuring that images are rendered sharply on devices with varying pixel densities.
- d.Converting abstract layout units into precise physical pixel dimensions for all UI elements.
Why? this is the answer
The card states that PixelRatio.getFontScale() returns the user's preferred font size multiplier from the OS accessibility settings, ensuring text is readable for all users. Option C describes the function of PixelRatio.get(), not getFontScale().
Just read this? Test yourself on what you have been reading.
Read the original → reactnative.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react-native — each one lists the topics its interview covers.
See open roles