In the map form of useFonts, every key is its own family, so a regular and a bold cut become two names you switch between by hand. SDK 58 can load several files under one fontFamily and select a face with the fontWeight and fontStyle style props, and Android and iOS now support variable fonts, which hold many faces in one file. Both change how you declare a typeface, and Android and iOS pick faces in different ways.
Key terms
- FontFamilyDefinition: an object with a
fontFamilyname and afontDefinitionsarray listing the faces that make up that family. - FontFaceDefinition: one entry in
fontDefinitions, with a requiredpathin anyFontSourceformat and optionalweightandstyle. - Variable font: a font file that holds many faces, so you ship one file and select a face with
fontWeightandfontStyle. - Named instance: a face that the font designer gave a name and a fixed position on each axis, such as a "Bold" face.
Several files, one family name
To load several files under one name, pass useFonts an array of FontFamilyDefinition objects instead of a map. A face's style is 'normal', 'italic' or 'oblique', and its weight is a number or a string, with 'normal' and 'bold' converted to 400 and 700. The Halcyon root layout below declares a regular, an italic and a bold face. A <Text> then selects a face with { fontFamily: 'Halcyon', fontWeight: '700' } or adds fontStyle: 'italic'.
Declare weight and style on every face of a multi-face family. Web needs both values, and explicit values keep the three platforms in agreement. Mistakes in the array reject with ERR_FONT_API: the same fontFamily declared twice in one call, two faces of a family with the same weight and style, or an empty fontDefinitions array.
Android uses the declared values and falls back to the ones in the font file. iOS matches faces by the weight and style metadata in the files, and uses the declared values only to pick the default face when a file has no readable metadata. Below API 29 (Android 10), only the default face of a runtime-loaded family loads. Families declared with the object form of the plugin's android.fonts use Android's XML font resources and don't have this limit.
Declare every face in one call
A fontFamily loads once. When a name is already loaded, a later loadAsync or useFonts call for it resolves without replacing it and adds none of its faces, even if that call lists more. This holds across the map and array forms. If the root layout calls useFonts({ Halcyon: require('@/assets/fonts/Halcyon-Regular.ttf') }), a settings screen that later passes an array adding an italic face to Halcyon loads nothing. Put all of a family's faces in one FontFamilyDefinition, in one call.
Variable fonts on each platform
Variable fonts work on Android and iOS from SDK 58, and both the config plugin and useFonts accept them. The platforms read the file differently:
- Android reads the
wghtaxis and draws each weight thatfontWeightcan name, within the range the axis declares. That needs Android 10 or later. Earlier versions render the default weight and create bold by thickening the letters. - iOS reads the named instances in the file and selects the one whose weight is closest to the request. A weight no named instance covers is out of reach, and a file with no named instances renders only its default face.
Loaded with useFonts, a single variable file is one face with style unset and weight unset. A single declared weight pins a variable face to that weight on Android, which synthesizes the others. useFonts reads only the weight axis. React Native 0.88 adds the fontVariationSettings style prop, which sets any axis on <Text> and <TextInput> and takes precedence over fontWeight and fontStyle.
The axes field on Android
SDK 58 added variable weight fonts and an axes field to the config plugin's Android font definitions. The plugin config below backs a whole family with one variable file, given once as the family-level path.
Each definition needs a weight, a whole number from 1 to 1000, plus an optional style of "normal" or "italic", and no two definitions in a family may share a weight and style pair. The plugin throws otherwise, because Android matches a family on weight and style alone and the app would crash at startup. weight and style pick the face that fontWeight and fontStyle match, and axes draws it, with wght defaulting to weight. style alone doesn't slant the glyphs, so the italic definition sets slnt. Any other axis the font declares, such as wdth for a condensed face, goes in axes the same way.
On iOS, path and axes don't apply. List the file in ios.fonts, select among its named instances, and reach other axis positions with fontVariationSettings.
Code Examples
import { useFonts } from 'expo-font';
import { Stack } from 'expo-router';
export default function RootLayout() {
const [loaded, error] = useFonts([
{
fontFamily: 'Halcyon',
fontDefinitions: [
{ path: require('@/assets/fonts/Halcyon-Regular.ttf'), weight: 400, style: 'normal' },
{ path: require('@/assets/fonts/Halcyon-Italic.ttf'), weight: 400, style: 'italic' },
{ path: require('@/assets/fonts/Halcyon-Bold.ttf'), weight: 700, style: 'normal' },
],
},
]);
if (!loaded && !error) {
return null;
}
return <Stack />;
}{
"expo": {
"plugins": [
[
"expo-font",
{
"android": {
"fonts": [
{
"fontFamily": "Halcyon",
"path": "./assets/fonts/HalcyonVariable.ttf",
"fontDefinitions": [
{ "weight": 400 },
{ "weight": 700 },
{ "weight": 400, "style": "italic", "axes": { "slnt": -10 } }
]
}
]
},
"ios": {
"fonts": ["./assets/fonts/HalcyonVariable.ttf"]
}
}
]
]
}
}