Skip to content

Instantly share code, notes, and snippets.

@Stringsaeed
Created February 19, 2022 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stringsaeed/0eb3098b545c08a281ed860badf448bc to your computer and use it in GitHub Desktop.
Save Stringsaeed/0eb3098b545c08a281ed860badf448bc to your computer and use it in GitHub Desktop.
Generic Fonts to use across react native app
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