Created
September 8, 2018 16:29
-
-
Save steniowagner/795d812d253e4b59172470b7aa41c02e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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