gpt4 book ai didi

react-native - React Native Expo 不加载字体

转载 作者:行者123 更新时间:2023-12-04 12:43:43 26 4
gpt4 key购买 nike

在这里 react 本地新人!

我正在尝试使用 expo 在我的 native 应用程序中使用自定义字体。我尝试按照 https://docs.expo.io/versions/latest/guides/using-custom-fonts.html#using-custom-fonts 上的说明进行操作没有运气。

这是我的 App.js:

import React from 'react';
import { View, Text, TouchableOpacity, TextInput, StyleSheet, AsyncStorage, Alert, Platform, NativeModules } from 'react-native';
import { Expo } from 'expo';

import { StackNavigator } from 'react-navigation';
import LoginScreen from './src/views/LoginScreen';
import AuthenticatedScreen from './src/views/AuthenticatedScreen';

import './ReactotronConfig'

import styles from './src/styles/ParentStyles'

const { StatusBarManager } = NativeModules;

const RootStack = StackNavigator(
{
Login: { screen: LoginScreen },
Authenticated: { screen: AuthenticatedScreen }
},
{ initialRouteName: 'Login'}
);

const STATUSBAR_HEIGHT = Platform.OS === 'ios' ? 20 : StatusBarManager.HEIGHT;

export default class App extends React.Component {
state = {
fontLoaded: false,
};

async componentDidMount() {
await Expo.Font.loadAsync({
'open-sans-bold': require('./src/assets/fonts/OpenSans-Bold.ttf'),
});
console.log('running')
this.setState({ fontLoaded: true });
}

render() {
console.log('statusBarHeight: ' + StatusBarManager.HEIGHT);
return (<RootStack />);
}
}

console.disableYellowBox = true;

我正在尝试拨打 open-sans-bold在我的登录页面中,如下所示:
render() {
return (
<View style = { parentStyles.container } >
<View style={ loginStyles.backgroundImageContainer }>
<Image style={ loginStyles.backgroundImage } source={require('../assets/img/splash.png')} />
</View>
<View style={ loginStyles.logoImageContainer } >
<Image style={ loginStyles.logoImage } source={require('../assets/img/PMlogo.png')} resizeMode="contain"/>
</View>
<View style={{flex: 50 }} >
//***CALLING FONT HERE***
<Text style={{ fontFamily: 'open-sans-bold', fontSize: 56 }}>Email</Text>
<TextInput style = {loginStyles.input}
underlineColorAndroid = "transparent"
placeholder = "Email"
placeholderTextColor = "red"
autoCapitalize = "none"
ref = { input => { this.textInputEmail = input }}
onChangeText = { this.handleEmail }/>

<TextInput style = { loginStyles.input }
underlineColorAndroid = "transparent"
placeholder = "Password"
placeholderTextColor = "red"
autoCapitalize = "none"
ref = { input => { this.textInputPassword = input }}
onChangeText = { this.handlePassword }/>

<TouchableOpacity
style = {loginStyles.submitButton}
onPress = { () => this.login(this.state.email, this.state.password) }
>
<Text style = { loginStyles.submitButtonText }> Login </Text>
</TouchableOpacity>
</View>
</View>
)

}

不幸的是,当我运行它时,我收到以下错误:
fontFamily 'open-sans-bold' is not a system font and has not been loaded through Expo.Font.loadAsync.

- If you intended to use a system font, make sure you typed the name correctly and that it is supported by your device operating system.

- If this is a custom font, be sure to load it with Expo.Font.loadAsync.

任何帮助都非常感谢!!

最佳答案

嘿兄弟,这是适用于 app.js 的正确代码,仅适用于您的情况

import React from "react";
import { AppLoading, Font } from "expo";
import { StyleSheet, Text, View } from "react-native";

export default class App extends React.Component {
state = {
loaded: false,
};

componentWillMount() {
this._loadAssetsAsync();
}

_loadAssetsAsync = async () => {
await Font.loadAsync({
diplomata: require("./assets/fonts/DiplomataSC-Regular.ttf"),
});
this.setState({ loaded: true });
};

render() {
if (!this.state.loaded) {
return <AppLoading />;
}

return (
<View style={styles.container}>
<Text style={styles.info}>
Look, you can load this font! Now the question is, should you use it?
Probably not. But you can load any font.
</Text>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
padding: 30,
},
info: {
fontFamily: "diplomata",
textAlign: "center",
fontSize: 14,
},
});

关于react-native - React Native Expo 不加载字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50256786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com