Skip to content

Instantly share code, notes, and snippets.

@douglira
Last active February 25, 2021 17:32
Show Gist options
  • Save douglira/52598ee893c65e31abc8e513de989d55 to your computer and use it in GitHub Desktop.
Save douglira/52598ee893c65e31abc8e513de989d55 to your computer and use it in GitHub Desktop.
React Native normalize font scale
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) }
}
@JDMathew
Copy link

What is the -2 doing in android?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment