How to set default font family in React Native?

后端 未结 10 1011
鱼传尺愫
鱼传尺愫 2020-11-29 00:25

Is there an equivalent to this CSS in React Native, so that the app uses the same font everywhere ?

body {
  font-family: \'Open Sans\';
}

10条回答
  •  佛祖请我去吃肉
    2020-11-29 00:59

    Super late to this thread but here goes.

    TLDR; Add the following block in your AppDelegate.m

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
     ....
        // HERE: replace "Verlag" with your font
      [[UILabel appearance] setFont:[UIFont fontWithName:@"Verlag" size:17.0]];
      ....
    }
    

    Walkthrough of the whole flow.

    A few ways you can do this outside of using a plugin like react-native-global-props so Ill walk you though step by step.

    Adding fonts to platforms.

    How to add the font to IOS project

    First let's create a location for our assets. Let's make the following directory at our root.

    ```

    ios/
    static/
           fonts/
    

    ```

    Now let's add a "React Native" NPM in our package.json

      "rnpm": {
        "static": [
       "./static/fonts/"
        ]
      }
    

    Now we can run "react-native link" to add our assets to our native apps.

    Verifying or doing manually.

    That should add your font names into the projects .plist (for VS code users run code ios/*/Info.plist to confirm)

    Here let's assume Verlag is the font you added, it should look something like this:

         
       
          .....
          UIAppFonts
          
             Verlag Bold Italic.otf
             Verlag Book Italic.otf
             Verlag Light.otf
             Verlag XLight Italic.otf
             Verlag XLight.otf
             Verlag-Black.otf
             Verlag-BlackItalic.otf
             Verlag-Bold.otf
             Verlag-Book.otf
             Verlag-LightItalic.otf
          
          ....    
    
    
    

    Now that you mapped them, now let's make sure they are actually there and being loaded (this is also how you'd do it manually).

    Go to "Build Phase" > "Copy Bundler Resource", If it didn't work you'll a manually add under them here.

    1_uuz0__3kx2vvguz37bhvya

    Get Font Names (recognized by XCode)

    First open your XCode logs, like:

    Then you can add the following block in your AppDelegate.m to log the names of the Fonts and the Font Family.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        .....
    
    
      for (NSString* family in [UIFont familyNames])
      {
        NSLog(@"%@", family);
        for (NSString* name in [UIFont fontNamesForFamilyName: family])
        {
          NSLog(@" %@", name);
        }
      }
    
      ...
    }
    

    Once you run you should find your fonts if loaded correctly, here we found ours in logs like this:

    2018-05-07 10:57:04.194127-0700 MyApp[84024:1486266] Verlag
    2018-05-07 10:57:04.194266-0700 MyApp[84024:1486266]  Verlag-Book
    2018-05-07 10:57:04.194401-0700 MyApp[84024:1486266]  Verlag-BlackItalic
    2018-05-07 10:57:04.194516-0700 MyApp[84024:1486266]  Verlag-BoldItalic
    2018-05-07 10:57:04.194616-0700 MyApp[84024:1486266]  Verlag-XLight
    2018-05-07 10:57:04.194737-0700 MyApp[84024:1486266]  Verlag-Bold
    2018-05-07 10:57:04.194833-0700 MyApp[84024:1486266]  Verlag-Black
    2018-05-07 10:57:04.194942-0700 MyApp[84024:1486266]  Verlag-XLightItalic
    2018-05-07 10:57:04.195170-0700 MyApp[84024:1486266]  Verlag-LightItalic
    2018-05-07 10:57:04.195327-0700 MyApp[84024:1486266]  Verlag-BookItalic
    2018-05-07 10:57:04.195510-0700 MyApp[84024:1486266]  Verlag-Light
    

    So now we know it loaded the Verlag family and are the fonts inside that family

    • Verlag-Book
    • Verlag-BlackItalic
    • Verlag-BoldItalic
    • Verlag-XLight
    • Verlag-Bold
    • Verlag-Black
    • Verlag-XLightItalic
    • Verlag-LightItalic
    • Verlag-BookItalic
    • Verlag-Light

    These are now the case sensitive names we can use in our font family we can use in our react native app.

    Got -'em now set default font.

    Then to set a default font to add your font family name in your AppDelegate.m with this line

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
     ....
        // ADD THIS LINE (replace "Verlag" with your font)
    
      [[UILabel appearance] setFont:[UIFont fontWithName:@"Verlag" size:17.0]];
      ....
    }
    

    Done.

提交回复
热议问题