How to use React Native PixelRatio utility class?

damax picture damax · Mar 9, 2016 · Viewed 10.1k times · Source

I have an app written initially for iPhone 6 symulator which has a componend syled with following example values:

const styles = StyleSheet.create({
  headerNav: {
    width: 40,
    height: 40
  },
  headerLogoImage: {
    width: 140,
    height: 140
  },
  footerNavText: {
    padding: 15,
    fontSize: 25
  }
});

Unfortunately when I launched the app on iPad symulator, the size proportions completely collapsed. I know that there is something like PixelRation but documentation is very limited and unclear.

Any idea / suggestions how can I translate these width / height / padding & fontSize to proper values using this PixelRatio class?

Answer

Marianna Panteli picture Marianna Panteli · May 5, 2017

You could do something like:

footerNavText: {
  padding: PixelRatio.get()*3,
  fontSize: PixelRatio.get()*4
}

Check what get() method returns for each of the devices you wish to use and style accordingly. For more info visit https://facebook.github.io/react-native/docs/pixelratio.html