react-native - React Native 异步字体加载
问题描述
我正在使用 react-native-cli 构建一个 React 本机代码项目,无论如何都可以在没有 Expo 的情况下异步加载字体?在我的 cli 本机项目中从“expo”导入 { Font } 会不会有问题?
解决方案
如果你想使用外部字体(如谷歌字体等)或任何矢量图标(如蚂蚁图标),你必须在应用渲染之前加载,像这样
import * as Font from "expo-font";
import { AppLoading } from "expo";
async componentDidMount() {
await Font.loadAsync(
"antoutline",
require("@ant-design/icons-react-native/fonts/antoutline.ttf")
);
await Font.loadAsync(
"antfill",
require("@ant-design/icons-react-native/fonts/antfill.ttf")
);
this.setState({ isReady: true });
}
render() {
const { theme, currentTheme, isReady } = this.state;
if (!isReady) {
return <AppLoading />;
}
const Loader = createAppContainer;
return (
<Provider theme={theme}>
<Loader />
</Provider>
);
}
推荐阅读
- reactjs - React Hooks - 默认情况下哪些是异步的?
- c++ - 在应用程序中读取 Docker 容器状态
- python - 如何降低子集和问题的时间复杂度
- c# - Visual Studio 2019 for Mac 在 Mac OS 上运行的 C# 代码无法写入/读取本地 Azure 存储帐户模拟器
- swift - 导入 UIKit.UIViewController 而不是导入整个框架的好处
- ansible - 如何遍历字典列表及其内容列表
- angular - 销毁组件时是否应该手动关闭 EventEmitter?
- javascript - React Redux reducer 未更新状态
- python - 值查看/修改窗口
- c# - ACTIVEMQ + NMS 无法同步接收