简体   繁体   English

如何将expo react-native自定义字体应用到整个容器

[英]How to apply expo react-native custom font to the entire container

I tried to load a custom font for my react-native app that i'm developing with expo but i don't know how to load a font in a simplier way for the whole screen container.我尝试为我正在使用 expo 开发的 react-native 应用程序加载自定义字体,但我不知道如何以更简单的方式为整个屏幕容器加载字体。

Currently i used the offical expo doc:Expo Custom Font Documentation目前我使用官方 expo 文档:Expo 自定义字体文档

They said to use a Font.loadAsync() function and then use the this.state.fontLoaded?他们说使用 Font.loadAsync() function 然后使用this.state.fontLoaded? like this:像这样:

    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
  {
    this.state.fontLoaded ? (
      <Text style={{ fontFamily: 'open-sans-bold', fontSize: 56 }}>
        Hello, world!
      </Text>
    ) : null
  }
</View>

but did it exist a solution for applying the font on a container for example?但它是否存在例如在容器上应用字体的解决方案? I think it's not easy to e forced to surround EACH Text elements with the same function...我认为用相同的 function 强制包围每个文本元素并不容易......

Currently the font is loading on ONE text element, but i would like to be able to easily use my font on a container, or many Text elements at once.目前字体正在一个文本元素上加载,但我希望能够轻松地在容器上使用我的字体,或者一次使用多个文本元素。

Here is my code:这是我的代码:

    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>
        );
    }
}

Finally i found a nicely working solution.最后我找到了一个很好的解决方案。

I had to create a custom component like this one:我必须创建一个像这样的自定义组件:

1. Create a custom component TextCustom.js for example and put this into: 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'
    },
})

Don't forget to import Font from Expo (for people using Expo)不要忘记从 Expo 导入字体(对于使用 Expo 的人)

2. in the component you want to use the custom font, simply import and use the newly created component: 2.在要使用自定义字体的组件中,只需导入并使用新创建的组件即可:

import TextCustom from './TextCustom'
import TextBoldCustom from './TextBoldCustom'  (if you want to use a Bold or italic font)

and use it:并使用它:

<View>
    <TextCustom style={styles.info_welcome}>Bonjour</TextCustom>
</View>

The documentation says there is no way to set the font across the entire app.文档说没有办法在整个应用程序中设置字体。

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.在您的应用程序中使用一致的 fonts 和大小的推荐方法是创建一个包含它们的组件 MyAppText 并在您的应用程序中使用此组件。 You can also use this component to make more specific components like MyAppHeaderText for other kinds of text.您还可以使用此组件为其他类型的文本制作更具体的组件,例如 MyAppHeaderText。

More info and example on the Expo <Text> docs:有关 Expo <Text>文档的更多信息和示例:

https://docs.expo.io/versions/latest/react-native/text/ https://docs.expo.io/versions/latest/react-native/text/

-- --

As far as loading custom font files, I'd recommend using SplashScreen.preventAutoHide() to keep the splash screen visible while you load the fonts in a function via <AppLoading> .至于加载自定义字体文件,我建议使用SplashScreen.preventAutoHide()在通过<AppLoading>在 function 中加载 fonts 时保持启动画面可见。 This provides a smooth loading experience, and ensures your fonts are available when the rest of your app is eventually rendered.这提供了流畅的加载体验,并确保在最终呈现您的应用程序的 rest 时,您的 fonts 可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM