5.0.0 - How can customize the theme font
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!
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 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.
@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?
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),
}
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 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.
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 That makes sense. I thought configureFonts method in V5 does the above code snippet.
@lukewalczak That makes sense. I thought configureFonts method in V5 works as the above code snippet.
Thanks!
Closing via: https://github.com/callstack/react-native-paper/pull/3462.
PLEASE update documentation to reflect this. It's very unclear. 🙏🙏🙏🙏
Thanks @lukewalczak for providing a solution. It would really help to have this in the doc :)
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?