Skip to content

custom font weight not working #4307

@ranjkkum

Description

@ranjkkum

I am trying to install custom fonts to a react native paper theme. Looked at the example mentioned on #3325 on the snack expo https://snack.expo.dev/@react-native-paper/font-styles-variants

i have implemented the below code after which the components picks up the fontFamily but the fontWeight does not change unless I specify the font family in the StyleSheet.

Can someone suggest what is wrong here.

import React from 'react';
import Toast, { BaseToast } from 'react-native-toast-message';
import { McIcon } from '@repo/mobile-ui';
import { Provider as PaperProvider, configureFonts } from 'react-native-paper';

import { Text } from 'react-native';
import { ThemeStatus, srgb } from '@repo/shared/utils';
import { DefaultTheme } from 'react-native-paper';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import AppNavigationShell from '../navigation/AppNavgationShell';
import { useFonts } from 'expo-font';

const theme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    
  },
};



export const App = () => {
  const [fontsLoaded] = useFonts({
    nsr: require('../../assets/fonts/NunitoSans_10pt-Regular.ttf'),
    nsrBold: require('../../assets/fonts/NunitoSans_10pt-Bold.ttf'),
    nsrItalic: require('../../assets/fonts/NunitoSans_10pt-Italic.ttf'),
    nsrBoldItalic: require('../../assets/fonts/NunitoSans_10pt-BoldItalic.ttf'),
  });

  const baseFont = {
    fontFamily: 'nsr',
  } as const;

  const baseVariants = configureFonts({ config: baseFont });
  const customVariants = {
    displayMedium: {
      ...baseVariants.displayMedium,
      fontFamily: 'nsr',
      fontWeight: 'normal',
    },

    bold: {
      ...baseVariants.bodyMedium,
      fontFamily: 'nsrBold',
      fontWeight: 'bold',
    },
    italic: {
      ...baseVariants.bodyMedium,
      fontFamily: 'nsrItalic',
      fontWeight: 'normal',
    },
    boldItalic: {
      ...baseVariants.bodyMedium,
      fontFamily: 'nsrBoldItalic',
      fontWeight: 'bold',
    },
  } as const;

  const mergedFonts = configureFonts({
    config: {
      ...baseVariants,
      ...customVariants,
    },
  });

  if (!fontsLoaded) {
    return <Text>Loading...</Text>;
  }
  return (
    <PaperProvider theme={{ ...theme, fonts: mergedFonts }}>
      <GestureHandlerRootView style={{ flex: 1 }}>
        <AppNavigationShell />
        <Toast config={toastConfig} />
      </GestureHandlerRootView>
    </PaperProvider>
  );
};

export default App;

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypographyquestionQuestion related to the library, not an issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions