How to add custom font in react native android

后端 未结 13 1275
别那么骄傲
别那么骄傲 2020-12-01 12:28

I am learning react-native, trying to create some demo apps just for learning. I want to set fontFamily to roboto thin of my toolbar title.

<
13条回答
  •  难免孤独
    2020-12-01 13:12

    Adding Custom Font with EXPO

    If you're using React Native chances are that you are using Expo as well. If that's the case, then you can load custom fonts using Expo's Font.loadAsync method.

    Steps

    1. Move your font to asset/fonts folder
    2. From the target component (for example: App.js) load Expo's Font module:
    import { Font } from 'expo'
    
    1. Set a state
    this.state = {
       fontLoaded: false
    }
    
    1. Load the custom font using componentDidMount:
    async componentDidMount() {
       await Font.loadAsync({
           'ComicSansBold': require('../assets/fonts/ComicSansMSBold.ttf'),
       })
    
       this.setState({fontLoaded: true})
    }
    
    1. Finally, use the style attribute to apply the desired font on a component:
    {
      this.state.fontLoaded
      ? Glad to Meet You!
      : null
    }
    

    Enjoy Coding....

    My Output:

提交回复
热议问题