Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
react native - get font scale
import {PixelRatio, Platform} from 'react-native';
type fontScales = -3 | -2 | -1 | 0 | 1 | 2 | 3;
type iOS_fontScales = '0.823' | '0.882' | '0.941' | '1' | '1.118' | '1.235' | '1.353';
type iOS_fontScalesMap<T> = {[scale in iOS_fontScales]: T};
const fontScale_iOS: iOS_fontScalesMap<number> = {
'0.823': -3,
'0.882': -2,
'0.941': -1,
'1': 0,
'1.118': 1,
'1.235': 2,
'1.353': 3,
};
type android_fontScales = '0.8500000238418579' | '1' | '1.149999976158142' | '1.2999999523162842';
type android_fontScalesMap<T> = {[scale in android_fontScales]: T};
const fontScale_android: android_fontScalesMap<number> = {
'0.8500000238418579': -1,
'1': 0,
'1.149999976158142': 1,
'1.2999999523162842': 2,
};
export const getFontScaleUtil = (): fontScales => {
const fontScaleFromPixelRatio = PixelRatio.getFontScale();
return Platform.select({
ios: fontScale_iOS[String(fontScaleFromPixelRatio) as iOS_fontScales],
android: fontScale_android[String(fontScaleFromPixelRatio) as android_fontScales],
default: 0,
}) as fontScales;
};
export default getFontScaleUtil;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment