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

@carimus/react-native-fonts

v1.0.0

Published

Utilities for react native to make using custom fonts easier.

Downloads

6,014

Readme

@carimus/react-native-fonts

A React Native package that provides some basic utilities and tools for working with custom fonts.

Motivation

React Native has a deficiency in how it handles custom fonts. There's lots of workarounds but if you use custom fonts in a lot of places in the app, they can get cumbersome.

This repository aims to provider helpers that make it easy to generate platform-specific Text Style Props for custom fonts, esp. those that use different font files for all permutations of weight and style.

Example

First, ensure you're font files are stored in your project's assets directory which you set via react-native.config.js and that you've run react-native link in order to link the font files into your native iOS and Android code.

Then you need to create a FontRegistry by giving the createFontRegistry function a Map of font family names to an array of their files. The files are defined in the form of FontFileDefinition objects which describe when a font should be picked and how it should be referenced within the RN styles (which is platform-dependent).

Once you have a font registry you can use its createFontStyles method to generate platform-specific font styles for a font family you've registered and for any weight and style variation (the closest match is found among the files you've defined).

import { View, Text } from 'react-native';
import { createFontRegistry } from '@carimus/react-native-fonts';

const fonts = new Map([
    [
        'Montserrat',
        [
            { name: 'Montserrat-Regular', weight: 400, style: 'normal' },
            { name: 'Montserrat-Italic', weight: 400, style: 'italic' },
            { name: 'Montserrat-Bold', weight: 700, style: 'normal' },
            { name: 'Montserrat-BoldItalic', weight: 700, style: 'italic' },
            { name: 'Montserrat-ExtraBold', weight: 800, style: 'normal' },
            {
                name: 'Montserrat-ExtraBoldItalic',
                weight: 800,
                style: 'italic',
            },
            { name: 'Montserrat-Light', weight: 300, style: 'normal' },
            { name: 'Montserrat-LightItalic', weight: 300, style: 'italic' },
            { name: 'Montserrat-Thin', weight: 250, style: 'normal' },
            { name: 'Montserrat-ThinItalic', weight: 250, style: 'italic' },
        ],
    ],
]);

const { createFontStyles } = createFontRegistry(fonts);

function MyComponent() {
    return (
        <View>
            <Text style={createFontStyles('Montserrat')}>Some normal text</Text>
            <Text style={createFontStyles('Montserrat', { weight: 700 })}>
                Some bold text
            </Text>
            <Text
                style={{
                    ...createFontStyles('Montserrat', {
                        weight: 700,
                        style: 'italic',
                    }),
                    color: '#FF0000',
                }}
            >
                Some bold, italic, red text.
            </Text>
        </View>
    );
}

Important Notes

  • You can use FontDrop! to quickly get info like the weight (usWeightClass), style (fontSubfamily kinda, you have to infer; "Heavy Italic" or anything containing the word "Italic" translate to 'italic'; everything else like "Regular", "Heavy", or "Black" etc. translates to 'normal'), and postScriptName.
  • iOS requires you use the postScriptName when you reference a fontFamily in RN Text Style Props but in Android, you have to use the font's filename without the extension. So in general, you should name your font files such that the file name w/o the extension is the same as the postScriptName of the font. If you do so you can use the name property in the font file definition object as shown in the example above with "Montserrat". If the filename differs for whatever reason from the postScriptName, you must explicitly specify postScriptName and fileName instead.
  • The weight must be a number between 100 and 900 in increments of 50.
  • The style must be either 'normal' or italic.

TODO

  • [ ] More detailed documentation instead of just an example.