Created
October 23, 2020 03:32
-
-
Save sturmenta/a960b9ca773762376adc667d05e42f4a to your computer and use it in GitHub Desktop.
react native - get font scale
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 {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