Last active
February 25, 2021 17:32
-
-
Save douglira/52598ee893c65e31abc8e513de989d55 to your computer and use it in GitHub Desktop.
React Native normalize font scale
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: SCREEN_WIDTH, | |
height: SCREEN_HEIGHT, | |
} = Dimensions.get('window'); | |
// based on iphone 5s's scale | |
const scale = SCREEN_WIDTH / 320; | |
export function normalize(size) { | |
const newSize = size * scale | |
if (Platform.OS === 'ios') { | |
return Math.round(PixelRatio.roundToNearestPixel(newSize)) | |
} else { | |
return Math.round(PixelRatio.roundToNearestPixel(newSize)) - 2 | |
} | |
} | |
const styles = { | |
text: { fontSize: normalize(24) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the -2 doing in android?