react-native-paper icon indicating copy to clipboard operation
react-native-paper copied to clipboard

5.0.0 - How can customize the theme font

Open jeremyfalldev opened this issue 4 years ago • 13 comments

Ask your Question

How can customize the theme font? I cannot find the details to customize the theme font in the document. https://callstack.github.io/react-native-paper/fonts.html

The old (v3) document shows the way to customize the theme font. But it does not work on v5. https://callstack.github.io/react-native-paper/3.0/fonts.html

Let me know, thanks!

jeremyfalldev avatar Nov 07 '22 03:11 jeremyfalldev

Newer version of React links assets automatically. You don't need to do anything more... First of all, link your fonts as what said in v5 and then, use the same way in v3 font configuration.

fmohtadi99 avatar Nov 07 '22 16:11 fmohtadi99

@fmohtadi99 Yes, we don't need to do anything to use the custom font. But have you ever tried to customize the theme font in V5? V3 font configuration does not work in V5.

jeremyfalldev avatar Nov 07 '22 16:11 jeremyfalldev

@fmohtadi99 Yes, we don't need to do anything to use the custom font. But have you ever tried to customize the theme font in V5? V3 font configuration does not work in V5.

Could you please upload your code?

fmohtadi99 avatar Nov 07 '22 16:11 fmohtadi99

import { configureFonts, MD3LightTheme } from 'react-native-paper';

const fontConfig = {
  default: {
    regular: {
      fontFamily: 'NotoSans-Regular',
      fontSize: 'normal',
    },
    medium: {
      fontFamily: 'NotoSans-Bold',
      fontWeight: 'normal',
    },
    light: {
      fontFamily: 'NotoSans-Regular',
      fontWeight: 'normal',
    },
    thin: {
      fontFamily: 'NotoSans-Regular',
      fontWeight: 'normal',
    },
  },
};

fontConfig.ios = fontConfig.default;
fontConfig.android = fontConfig.default;
fontConfig.web = fontConfig.default;

export default MyTheme = {
  ...MD3LightTheme,
  fonts: configureFonts(fontConfig),
}

jeremyfalldev avatar Nov 07 '22 17:11 jeremyfalldev

Hey @jeremyfalldev, could you please explain what are you trying to achieve? configureFonts doesn't work on the v3 since fonts structure differs between v2 and v3 - in v3 fonts are based on variants, where each of them contains the following properties: fontFamily, letterSpacing, fontWeight, lineHeight, fontSize.

lukewalczak avatar Nov 07 '22 21:11 lukewalczak

@lukewalczak I wanna use the NotoSans instead of System on all the react-native-paper components. This is the theme's fonts object. "fonts": {"bodyLarge": {"fontFamily": "System", "fontSize": 16, "fontWeight": "400", "letterSpacing": 0.15, "lineHeight": 24}, "bodyMedium": {"fontFamily": "System", "fontSize": 14, "fontWeight": "400", "letterSpacing": 0.25, "lineHeight": 20}, "bodySmall": {"fontFamily": "System", "fontSize": 12, "fontWeight": "400", "letterSpacing": 0.4, "lineHeight": 16}, "displayLarge": {"fontFamily": "System", "fontSize": 57, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 64}, "displayMedium": {"fontFamily": "System", "fontSize": 45, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 52}, "displaySmall": {"fontFamily": "System", "fontSize": 36, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 44}, "headlineLarge": {"fontFamily": "System", "fontSize": 32, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 40}, "headlineMedium": {"fontFamily": "System", "fontSize": 28, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 36}, "headlineSmall": {"fontFamily": "System", "fontSize": 24, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 32}, "labelLarge": {"fontFamily": "System", "fontSize": 14, "fontWeight": "500", "letterSpacing": 0.1, "lineHeight": 20}, "labelMedium": {"fontFamily": "System", "fontSize": 12, "fontWeight": "500", "letterSpacing": 0.5, "lineHeight": 16}, "labelSmall": {"fontFamily": "System", "fontSize": 11, "fontWeight": "500", "letterSpacing": 0.5, "lineHeight": 16}, "titleLarge": {"fontFamily": "System", "fontSize": 22, "fontWeight": "400", "letterSpacing": 0, "lineHeight": 28}, "titleMedium": {"fontFamily": "System", "fontSize": 16, "fontWeight": "500", "letterSpacing": 0.15, "lineHeight": 24}, "titleSmall": {"fontFamily": "System", "fontSize": 14, "fontWeight": "500", "letterSpacing": 0.1, "lineHeight": 20}}

Do I have to change all the fontFamily manually? Or is there any way to do it easily? Nothing is mentioned in the document to customize the theme font.

jeremyfalldev avatar Nov 08 '22 02:11 jeremyfalldev

Do I have to change all the fontFamily manually? Or is there any way to do it easily? Nothing is mentioned in the document to customize the theme font.

Partially, because you have to override the fontFamily in the theme fonts variants, but you can easily achieve it by modifying theme.fonts property, like below:

const customThemeFonts = Object.fromEntries(
  Object.entries(MD3LightTheme.fonts).map(
    ([variantName, variantProperties]) => [
      variantName,
      { ...variantProperties, fontFamily: 'NotoSans' },
    ]
  )
);

I've prepared the snack where I've presented the whole approach.

Looks like it can be helpful to provide some utility for that 🤔

lukewalczak avatar Nov 08 '22 09:11 lukewalczak

@lukewalczak That makes sense. I thought configureFonts method in V5 does the above code snippet.

jeremyfalldev avatar Nov 08 '22 09:11 jeremyfalldev

@lukewalczak That makes sense. I thought configureFonts method in V5 works as the above code snippet. Thanks!

jeremyfalldev avatar Nov 08 '22 18:11 jeremyfalldev

Closing via: https://github.com/callstack/react-native-paper/pull/3462.

lukewalczak avatar Nov 14 '22 09:11 lukewalczak

PLEASE update documentation to reflect this. It's very unclear. 🙏🙏🙏🙏

kristyincamas avatar Oct 25 '23 22:10 kristyincamas

Thanks @lukewalczak for providing a solution. It would really help to have this in the doc :)

obasille avatar Dec 01 '23 11:12 obasille

I'm sorry to re-open this. How can I use two script fonts? I'm working on app that it's multilingual (English, French, and Arabic) I want to use NotoSans for Latin script and NotoSansArabic for Arabic script. Is this possible?

mbougarne avatar Mar 28 '24 14:03 mbougarne