Skip to content

Instantly share code, notes, and snippets.

@gleydson
Last active March 1, 2024 06:15
Show Gist options
  • Save gleydson/0e778e834655d1ee177725d8b4b345d7 to your computer and use it in GitHub Desktop.
Save gleydson/0e778e834655d1ee177725d8b4b345d7 to your computer and use it in GitHub Desktop.
Functions that transform percentages into pixel density to react native
import { Dimensions } from 'react-native';
const widthPercentageToDP = widthPercent => {
const screenWidth = Dimensions.get('window').width;
const elemWidth = parseFloat(widthPercent);
return PixelRatio.roundToNearestPixel((screenWidth * elemWidth) / 100);
};
const heightPercentageToDP = heightPercent => {
const screenHeight = Dimensions.get('window').height;
const elemHeight = parseFloat(heightPercent);
return PixelRatio.roundToNearestPixel((screenHeight * elemHeight) / 100);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment