Skip to content

Instantly share code, notes, and snippets.

@steniowagner
Created September 8, 2018 16:29
Show Gist options
  • Save steniowagner/795d812d253e4b59172470b7aa41c02e to your computer and use it in GitHub Desktop.
Save steniowagner/795d812d253e4b59172470b7aa41c02e to your computer and use it in GitHub Desktop.
import { Dimensions, Platform, PixelRatio } from 'react-native';
const { width, height } = Dimensions.get('window');
const getWidthFromDP = (widthPercentage) => {
const percentageDesired = parseFloat(widthPercentage);
const widthPercentageToDP = PixelRatio.roundToNearestPixel((width * percentageDesired) / 100);
return widthPercentageToDP;
};
const getHeightFromDP = (heightPercentage) => {
const percentageDesired = parseFloat(heightPercentage);
const heightPercentageToDP = PixelRatio.roundToNearestPixel((height * percentageDesired) / 100);
return heightPercentageToDP;
};
// USAGE
marginLeft: getWidthFromDP('12%');
fontSize: getHeightFromDP('1%');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment