@carimus/react-native-fonts

中文文档
1.0.0发布于 2019/09/27MITCommonJS零依赖近 30 天下载 1793

Utilities for react native to make using custom fonts easier.

原始使用文档
文档目录4 节
  1. Motivation
  2. Example
  3. Important Notes
  4. TODO

@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.

版本与发布记录

@carimus/react-native-fonts 共发布 1 个版本,首次发布于 2019/09/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2019/09/277 年前

关于 @carimus/react-native-fonts 的常见问题

@carimus/react-native-fonts 是什么?

Utilities for react native to make using custom fonts easier.

如何安装 @carimus/react-native-fonts?

在项目目录运行 npm install @carimus/react-native-fonts,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@carimus/react-native-fonts 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/carimus__react-native-fonts 是否存在。

@carimus/react-native-fonts 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@carimus/react-native-fonts 可以商用吗?

@carimus/react-native-fonts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@carimus/react-native-fonts 的下载量有多少?

根据 npm 官方下载统计,@carimus/react-native-fonts 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,793 次,日均约 60 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@carimus/react-native-fonts 还在维护吗?

最新版本 1.0.0 发布于 2019/09/27(7 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。