如何将 expo react-native 自定义字体应用于整个容器
How to apply expo react-native custom font to the entire container
我试图为我正在使用 expo 开发的 react-native 应用程序加载自定义字体,但我不知道如何以更简单的方式为整个屏幕容器加载字体。
目前我使用的是官方展览文档:Expo Custom Font Documentation
他们说要使用 Font.loadAsync() 函数,然后使用
this.state.fontLoaded?
像这样:
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
{
this.state.fontLoaded ? (
<Text style={{ fontFamily: 'open-sans-bold', fontSize: 56 }}>
Hello, world!
</Text>
) : null
}
</View>
但是是否存在将字体应用到容器上的解决方案?我认为用相同的功能强制包围每个文本元素并不容易...
目前字体正在一个文本元素上加载,但我希望能够轻松地在容器上使用我的字体,或者同时在多个文本元素上使用我的字体。
这是我的代码:
state = {
fontLoaded: false,
};
async componentDidMount() {
await Font.loadAsync({
'ubuntu-medium': require('../assets/fonts/Ubuntu-Medium.ttf'),
});
this.setState({ fontLoaded: true });
}
render() {
return (
<View style={styles.main_container}>
<View style={styles.inner_main_container}>
<View style={styles.top_links_container}>
<View style={styles.profile_and_arrow_container}>
<Icon
name='arrow-back'
color='white'
style={styles.icon} />
{
this.state.fontLoaded ? (
<Text style={styles.top_links_profile}>Profil</Text>
) : null
}
</View>
<View style={styles.profile_edit_container}>
<Text style={styles.top_links_edit}>Editer</Text>
</View>
</View>
<View style={styles.profile_avatar_container}>
<Avatar
rounded
size='xlarge'
source={{ uri: 'https://randomuser.me/api/portraits/men/27.jpg' }}>
</Avatar>
</View>
<View style={styles.profile_infos_container}>
{
this.state.fontLoaded ? (
<Text style={styles.user_name}> Dupont Jean </Text>
) : null
}
<Text style={styles.user_title}> CSD - Product Owner </Text>
</View>
<View style={styles.subprofile_infos_container}>
<View style={styles.user_experience}>
<Text>Experience </Text>
<Text style={styles.user_experience_years}> 7ans</Text>
</View>
<View style={styles.user_grade}>
<Text>Grade </Text>
<Text style={styles.user_grade_letter}> D </Text>
</View>
</View>
<View numberOfLines={6}>
<Text style={styles.user_bio}> Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard…</Text>
</View>
<View>
<Text style={styles.user_bio_see_more_link}> Voir plus</Text>
</View>
<Divider style={styles.divider} />
<View style={styles.bottom_container}>
<View style={styles.bottom_cm_text_info_container}>
<Text style={styles.bottom_cm_text_info}>Carrière Manager d'Evelyne</Text>
<Text style={styles.bottom_cm_text_user_name}>Jerôme Durand</Text>
</View>
<View style={styles.bottom_cm_avatar}>
<Avatar
rounded
size='small'
source={{ uri: 'https://randomuser.me/api/portraits/men/27.jpg' }}
/>
<Icon
name='right'
type='antdesign'
color='grey'
onPress={() => console.log('hello button cm view')}
/>
</View>
</View>
</View>
</View>
);
}
}
我终于找到了一个很好用的解决方案。
我必须创建一个像这样的自定义组件:
1.例如创建一个自定义组件 TextCustom.js 并将其放入:
import React from 'react'
import { Text, StyleSheet, View, ActivityIndicator } from 'react-native'
import * as Font from 'expo-font'
export default class TextCustom extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
}
}
async componentWillMount() {
await Font.loadAsync({
'Ubuntu': require('./../assets/fonts/Ubuntu-Medium.ttf')
})
this.setState({ loading: false })
}
render() {
if (this.state.loading) {
return <ActivityIndicator/>
}
return (
<View>
<Text style={[styles.defaultStyle, this.props.style]}>
{this.props.children}
</Text>
</View>
)
}
}
const styles = StyleSheet.create({
defaultStyle: {
fontFamily: 'Ubuntu'
},
})
不要忘记从 Expo 导入字体(对于使用 Expo 的人)
2。在要使用自定义字体的组件中,只需导入并使用新创建的组件即可:
import TextCustom from './TextCustom'
import TextBoldCustom from './TextBoldCustom' (if you want to use a Bold or italic font)
并使用它:
<View>
<TextCustom style={styles.info_welcome}>Bonjour</TextCustom>
</View>
文档说无法在整个应用程序中设置字体。
The recommended way to use consistent fonts and sizes across your application is to create a component MyAppText that includes them and use this component across your app. You can also use this component to make more specific components like MyAppHeaderText for other kinds of text.
有关世博会的更多信息和示例 <Text>
文档:
https://docs.expo.io/versions/latest/react-native/text/
--
就加载自定义字体文件而言,我建议使用 SplashScreen.preventAutoHide()
以在通过 <AppLoading>
在函数中加载字体时保持初始屏幕可见。这提供了流畅的加载体验,并确保您的字体在您的应用程序的其余部分最终呈现时可用。
我试图为我正在使用 expo 开发的 react-native 应用程序加载自定义字体,但我不知道如何以更简单的方式为整个屏幕容器加载字体。
目前我使用的是官方展览文档:Expo Custom Font Documentation
他们说要使用 Font.loadAsync() 函数,然后使用
this.state.fontLoaded?
像这样:
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
{
this.state.fontLoaded ? (
<Text style={{ fontFamily: 'open-sans-bold', fontSize: 56 }}>
Hello, world!
</Text>
) : null
}
</View>
但是是否存在将字体应用到容器上的解决方案?我认为用相同的功能强制包围每个文本元素并不容易...
目前字体正在一个文本元素上加载,但我希望能够轻松地在容器上使用我的字体,或者同时在多个文本元素上使用我的字体。
这是我的代码:
state = {
fontLoaded: false,
};
async componentDidMount() {
await Font.loadAsync({
'ubuntu-medium': require('../assets/fonts/Ubuntu-Medium.ttf'),
});
this.setState({ fontLoaded: true });
}
render() {
return (
<View style={styles.main_container}>
<View style={styles.inner_main_container}>
<View style={styles.top_links_container}>
<View style={styles.profile_and_arrow_container}>
<Icon
name='arrow-back'
color='white'
style={styles.icon} />
{
this.state.fontLoaded ? (
<Text style={styles.top_links_profile}>Profil</Text>
) : null
}
</View>
<View style={styles.profile_edit_container}>
<Text style={styles.top_links_edit}>Editer</Text>
</View>
</View>
<View style={styles.profile_avatar_container}>
<Avatar
rounded
size='xlarge'
source={{ uri: 'https://randomuser.me/api/portraits/men/27.jpg' }}>
</Avatar>
</View>
<View style={styles.profile_infos_container}>
{
this.state.fontLoaded ? (
<Text style={styles.user_name}> Dupont Jean </Text>
) : null
}
<Text style={styles.user_title}> CSD - Product Owner </Text>
</View>
<View style={styles.subprofile_infos_container}>
<View style={styles.user_experience}>
<Text>Experience </Text>
<Text style={styles.user_experience_years}> 7ans</Text>
</View>
<View style={styles.user_grade}>
<Text>Grade </Text>
<Text style={styles.user_grade_letter}> D </Text>
</View>
</View>
<View numberOfLines={6}>
<Text style={styles.user_bio}> Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's standard…</Text>
</View>
<View>
<Text style={styles.user_bio_see_more_link}> Voir plus</Text>
</View>
<Divider style={styles.divider} />
<View style={styles.bottom_container}>
<View style={styles.bottom_cm_text_info_container}>
<Text style={styles.bottom_cm_text_info}>Carrière Manager d'Evelyne</Text>
<Text style={styles.bottom_cm_text_user_name}>Jerôme Durand</Text>
</View>
<View style={styles.bottom_cm_avatar}>
<Avatar
rounded
size='small'
source={{ uri: 'https://randomuser.me/api/portraits/men/27.jpg' }}
/>
<Icon
name='right'
type='antdesign'
color='grey'
onPress={() => console.log('hello button cm view')}
/>
</View>
</View>
</View>
</View>
);
}
}
我终于找到了一个很好用的解决方案。
我必须创建一个像这样的自定义组件:
1.例如创建一个自定义组件 TextCustom.js 并将其放入:
import React from 'react'
import { Text, StyleSheet, View, ActivityIndicator } from 'react-native'
import * as Font from 'expo-font'
export default class TextCustom extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
}
}
async componentWillMount() {
await Font.loadAsync({
'Ubuntu': require('./../assets/fonts/Ubuntu-Medium.ttf')
})
this.setState({ loading: false })
}
render() {
if (this.state.loading) {
return <ActivityIndicator/>
}
return (
<View>
<Text style={[styles.defaultStyle, this.props.style]}>
{this.props.children}
</Text>
</View>
)
}
}
const styles = StyleSheet.create({
defaultStyle: {
fontFamily: 'Ubuntu'
},
})
不要忘记从 Expo 导入字体(对于使用 Expo 的人)
2。在要使用自定义字体的组件中,只需导入并使用新创建的组件即可:
import TextCustom from './TextCustom'
import TextBoldCustom from './TextBoldCustom' (if you want to use a Bold or italic font)
并使用它:
<View>
<TextCustom style={styles.info_welcome}>Bonjour</TextCustom>
</View>
文档说无法在整个应用程序中设置字体。
The recommended way to use consistent fonts and sizes across your application is to create a component MyAppText that includes them and use this component across your app. You can also use this component to make more specific components like MyAppHeaderText for other kinds of text.
有关世博会的更多信息和示例 <Text>
文档:
https://docs.expo.io/versions/latest/react-native/text/
--
就加载自定义字体文件而言,我建议使用 SplashScreen.preventAutoHide()
以在通过 <AppLoading>
在函数中加载字体时保持初始屏幕可见。这提供了流畅的加载体验,并确保您的字体在您的应用程序的其余部分最终呈现时可用。