Skip to content

Instantly share code, notes, and snippets.

@ZainaliSyed
Last active April 13, 2018 07:11
Show Gist options
  • Save ZainaliSyed/a393a4dcb4be218ecb7b02f854b87b6c to your computer and use it in GitHub Desktop.
Save ZainaliSyed/a393a4dcb4be218ecb7b02f854b87b6c to your computer and use it in GitHub Desktop.

Formula for % Base calculation of width and height of a view in react native

import {Dimensions, PixelRatio} from 'react-native'; const widthPercentageToDP = widthPercent => { const screenWidth = Dimensions.get('window').width; // Convert string input to decimal number const elemWidth = parseFloat(widthPercent); return PixelRatio.roundToNearestPixel(screenWidth * elemWidth / 100); }; const heightPercentageToDP = heightPercent => { const screenHeight = Dimensions.get('window').height; // Convert string input to decimal number const elemHeight = parseFloat(heightPercent); return PixelRatio.roundToNearestPixel(screenHeight * elemHeight / 100); }; export { widthPercentageToDP, heightPercentageToDP };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment