This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 字体
一个允许在运行时加载字体并在 React Native 组件中使用它们的库。
expo-font 允许从网页加载字体,并在 React Native 组件中使用它们。更多详细的用法信息请参见 字体 指南。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
应用配置中的配置
有两种方式可以将字体添加到你的应用中:使用 expo-font 配置插件(推荐用于 Android 和 iOS)或在运行时加载它们(这在包括 web 在内的所有平台上都可用)。
在 Android 和 iOS 上,该插件允许你在构建时嵌入字体文件,这比 useFonts 或 loadAsync 更高效。设置好配置插件并运行 prebuild 后,你就可以立即渲染自定义字体。该插件可以通过不同方式进行配置,参见 字体 指南了解如何使用它。
Example app.json with config plugin
Configurable properties
Are you using this library in an existing React Native app?
- Android: 将字体文件复制到 android/app/src/main/assets/fonts。
- iOS: 请参阅 Apple Developer 文档中的 将自定义字体添加到你的应用。
用法
如果你不想使用 配置插件,你可以在运行时使用 useFonts Hook 加载字体,如下所示:
API
import * as Font from 'expo-font';
Constants
Type: UseFontHook
Load a map of fonts at runtime with loadAsync. This returns true if the fonts are
loaded and ready to use. It also returns an error if something went wrong, to use in development.
Note, the fonts are not "reloaded" when you dynamically change the font map.
Example
const [loaded, error] = useFonts({ 'Inter-Black': require('./assets/fonts/Inter-Black.otf'), });
Methods
Synchronously get all the fonts that have been loaded.
This includes fonts that were bundled at build time using the config plugin, as well as those loaded at runtime using loadAsync.
string[]Returns array of strings which you can use as fontFamily style prop.
Synchronously detect if the font for fontFamily has finished loading.
booleanReturns true if the font has fully loaded.
Synchronously detect if the font for fontFamily is still being loaded.
booleanReturns true if the font is still loading.
An efficient method for loading fonts from static or remote resources which can then be used
with the platform's native text elements. In the browser, this generates a @font-face block in
a shared style sheet for fonts. No CSS is needed to use this method.
Note: We recommend using the config plugin instead whenever possible.
Promise<void>Returns a promise that fulfils when the font has loaded. Often you may want to wrap the
method in a try/catch/finally to ensure the app continues if the font fails to load.
Creates an image with provided text.
Promise<RenderToImageResult>Promise which fulfils with image metadata.
Interfaces
Types
An object used to dictate the resource that is loaded into the provided font namespace when used
with loadAsync.
Literal type: union
The different types of assets you can provide to the loadAsync() function.
A font source can be a URI, a module ID, or an Expo Asset.
Acceptable values are: string | number | Asset | FontResource
Type: object shaped as below:
Or object shaped as below:
Enums
Sets the font-display
for a given typeface. The default font value on web is FontDisplay.AUTO.
Even though setting the fontDisplay does nothing on native platforms, the default behavior
emulates FontDisplay.SWAP on flagship devices like iOS, Samsung, Pixel, etc. Default
functionality varies on One Plus devices. In the browser this value is set in the generated
@font-face CSS block and not as a style property meaning you cannot dynamically change this
value based on the element it's used in.
FontDisplay.AUTO = "auto"(Default) The font display strategy is defined by the user agent or platform. This generally defaults to the text being invisible until the font is loaded. Good for buttons or banners that require a specific treatment.
FontDisplay.BLOCK = "block"The text will be invisible until the font has loaded. If the font fails to load then nothing will appear - it's best to turn this off when debugging missing text.
FontDisplay.FALLBACK = "fallback"Splits the behavior between SWAP and BLOCK.
There will be a 100ms timeout
where the text with a custom font is invisible, after that the text will either swap to the
styled text or it'll show the unstyled text and continue to load the custom font. This is good
for buttons that need a custom font but should also be quickly available to screen-readers.
FontDisplay.OPTIONAL = "optional"This works almost identically to FALLBACK, the only difference is that the browser will
decide to load the font based on slow connection speed or critical resource demand.