Last active
March 1, 2024 06:15
-
-
Save gleydson/0e778e834655d1ee177725d8b4b345d7 to your computer and use it in GitHub Desktop.
Functions that transform percentages into pixel density to react native
This file contains 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 } 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