Created
February 19, 2022 20:11
-
-
Save Stringsaeed/0eb3098b545c08a281ed860badf448bc to your computer and use it in GitHub Desktop.
Generic Fonts to use across react native app
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 {Platform, StyleSheet, TextStyle} from 'react-native'; | |
import {getDesignSize} from '@utils'; | |
type Weight = '700' | '600' | '500' | '400'; | |
const fonts = new Map<Weight, string>([ | |
['700' as const, Platform.select({default: '700' as const, android: 'Bold'})], | |
[ | |
'500' as const, | |
Platform.select({default: '500' as const, android: 'Medium'}), | |
], | |
[ | |
'400' as const, | |
Platform.select({default: '400' as const, android: 'Regular'}), | |
], | |
]); | |
export const generateTextStyle = ( | |
weight: Weight, | |
fontFamily: 'DM Sans', | |
fontSize?: number, | |
lineHeight?: number, | |
) => | |
Platform.select<TextStyle>({ | |
default: { | |
fontFamily, | |
fontWeight: fonts.get(weight) as TextStyle['fontWeight'], | |
...(fontSize && {fontSize: getDesignSize(fontSize)}), | |
...(lineHeight ? {lineHeight: getDesignSize(lineHeight)} : {}), | |
}, | |
android: { | |
fontFamily: `${fontFamily.split(' ').join('').trim()}-${fonts.get( | |
weight, | |
)}`, | |
includeFontPadding: false, | |
...(fontSize && {fontSize: getDesignSize(fontSize)}), | |
...(lineHeight ? {lineHeight: getDesignSize(lineHeight)} : {}), | |
}, | |
}); | |
const Fonts = StyleSheet.create({ | |
regular10: generateTextStyle('400', 'DM Sans', 10, 16), | |
regular12: generateTextStyle('400', 'DM Sans', 12, 16), | |
medium12: generateTextStyle('500', 'DM Sans', 12, 16), | |
regular14: generateTextStyle('400', 'DM Sans', 14, 20), | |
medium14: generateTextStyle('500', 'DM Sans', 14, 20), | |
bold14: generateTextStyle('700', 'DM Sans', 14, 20), | |
regular16: generateTextStyle('400', 'DM Sans', 16, 24), | |
medium16: generateTextStyle('500', 'DM Sans', 16, 24), | |
bold16: generateTextStyle('700', 'DM Sans', 16, 24), | |
bold18: generateTextStyle('700', 'DM Sans', 18, 28), | |
bold24: generateTextStyle('700', 'DM Sans', 24, 32), | |
bold32: generateTextStyle('700', 'DM Sans', 32, 35), | |
}); | |
export default Fonts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment