npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-font-faces

v4.1.4

Published

Easily emulate @font-face behavior in react-native

Downloads

582

Readme

React Native Font Faces

Easily emulate @font-face behavior in react-native.

Motivation:

Using custom fonts in React Native becomes complicated when trying to work with different font weights and styles. Even though the React Native TextStyle type includes properties for fontFamily, fontWeight and fontStyle, these properties seem to work only for the default built-in fonts, and have limited support when using custom fonts. For this reason, selecting a specific font weight and style is traditionally achieved by specifying the exact PostScript name of the desired loaded font file.

For example:

const style: ViewStyle = {
  fontFamily: 'Roboto-MediumItalic',
};

This makes it difficult to achieve merged styles or text style composition. A preferable solution might be something like this:

const style: ViewStyle = {
  fontFamily: 'Roboto',
  fontWeight: '500',
  fontStyle: 'italic',
};

This library aims to make life easier by allowing React Native developers to use fontWeight and fontStyle with custom fonts on iOS, Android, and Web.

Getting Started

  1. Add the required dependencies to your application's package.json:

    yarn add react-native-font-faces

    If you are using Expo and need to load additional custom font files into your app, also add the following:

    yarn add expo-font
  2. Add a call to enableFontFaces() in your application's entry point, and import the desired font faces. Then just use the font family as you would normally expect:

    // App.tsx
    
    import React from 'react';
    import { useFonts } from 'expo-font';
    import { AppLoading } from 'expo';
    import { AppContent } from './AppContent';
    import { Roboto_All, enableFontFaces, getExpoFontMap } from 'react-native-font-faces';
    
    enableFontFaces(Roboto_All);
    
    export default function App() {
      const [loaded, error] = useFonts(getExpoFontMap(Roboto_All));
    
      if (!loaded) {
        return <AppLoading />;
      } else if (error) {
        return <Text>{error.message}</Text>;
      } else {
        return (
          <View style={styles.container}>
            <StatusBar style="auto" />
            <Text style={styles.text}>This should be Regular</Text>
            <Text style={[styles.text, styles.italic]}>This should be Italic</Text>
            <Text style={[styles.text, styles.bold]}>This should be Bold</Text>
            <Text style={[styles.text, styles.bold, styles.italic]}>This should be BoldItalic</Text>
            <Text style={[styles.text, styles.thin]}>This should be Thin</Text>
            <Text style={[styles.text, styles.thin, styles.italic]}>This should be ThinItalic</Text>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      text: {
        fontFamily: 'Roboto',
      },
      bold: {
        fontWeight: 'bold',
      },
      thin: {
        fontWeight: '100',
      },
      italic: {
        fontStyle: 'italic',
      },
      container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
      },
    });

Migrating from 3.x

In version 4.x, we removed FontFacesProvider and added enableFontFaces. Follow these steps to migrate:

  1. Remove all instances of <FontFacesProvider />.
  2. Add a call to enableFontFaces() in your application's entrypoint.
  3. (Optional) Add a call to useFonts() (expo-font) or loadFonts() (react-native-dynamic-fonts) to dynamically load remote fonts.

Migrating from 2.x

In version 3.x, we simplified FontFacesProvider and removed useFontFaces. Follow these steps to migrate:

  1. Remove all instances of useFontFaces().
  2. Update your application's <FontFacesProvider/> to provide the onLoading and onError props (optional).

Migrating from 1.x

In version 2.x, we introduced FontFacesProvider and useFontFaces, and removed enableFontFaces. Follow these steps to migrate:

  1. Remove all instances of enableFontFaces().
  2. Add a <FontFacesProvider/> around your application's root component.
  3. Add const [fontsLoaded] = useFontFaces(...) inside an inner function component's body and handle the fontsLoaded value appropriately.