使用自定义字体来样式文本。

huangapple go评论93阅读模式
英文:

using custom fonts to style Text

问题

<Text style={{fontFamily:'Lora-SemiBold', fontSize:24, color:'#FFF'}}>让我们开始吧</Text>
英文:

I am building a component. I am quite new to styling.
I want to use a font (its already been setup for use) so i want to use that font inside my react native program.

The font is Lora Semibold. And using it inside a view, its supposed to be White and Bold up to size 36. Its just black and does not show the Expanded font, it just shows the black font and its default, real small.

My code is looking thus :

    import {Image, StyleSheet, Text, View} from &#39;react-native&#39;;
    import React from &#39;react&#39;;
    import {TouchableOpacity} from &#39;react-native&#39;;
    
    const NavigateNextPage = () =&gt; {
        navigation.navigate(&#39;WalkThruPage3&#39;);
      };
    
    const WalkthruBottom1 = () =&gt; {
      return (
        &lt;View style={styles.container}&gt;
        &lt;View style={{flex:1}}&gt;
        &lt;Text styles={{fontFamily:&#39;Lora-SemiBold&#39;, fontSize:24, color:&#39;#FFF&#39;}}&gt;Lets get Started&lt;/Text&gt;
        &lt;/View&gt;
          &lt;TouchableOpacity
            onPress={() =&gt; {
              NavigateNextPage();
            }}
            style={{
              position: &#39;absolute&#39;,
              backgroundColor: &#39;#DF2A54&#39;,
              borderRadius: 150,
              height: 100,
              width: 100,
              bottom: 50,
              right: 30,
              zIndex: 1,
            }}&gt;
            &lt;Image
              source={require(&#39;../../assets/images/LoginArrow.png&#39;)}
              style={styles.arrowImage}
            /&gt;
          &lt;/TouchableOpacity&gt;
        &lt;/View&gt;
      );
    };

export default WalkthruBottom1;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: &#39;#320F3B&#39;,
  },
  homeText: {
    fontFamily:&quot;Lora-SemiBold&quot;,
    fontSize: 50,
    color: &#39;#FFF&#39;
  },
  arrowImage:{
    height:50,
    width:50,
    top:25,
    alignSelf: &#39;center&#39;
  }
});

I want the Code on the Walk thru bottom "Lets get started" to use the font, how do I go about this?

答案1

得分: 3

从我所看到的情况来看,可能在应用样式到 '' 组件时,你在代码中犯了一个错误。正确的方式是使用 'style',而不是 'styles'。

  • 从:
    <Text styles={{fontFamily:'Lora-SemiBold', fontSize:24, color:'#FFF'}}>Lets get Started</Text>
  • 到:<Text style={{ fontFamily: 'Lora-SemiBold', fontSize: 24, color: '#FFF' }}>Lets get Started</Text>

如果直接将正确的样式应用到 '' 组件似乎不会改变字体,可能有一些原因。以下是一些你可以尝试的故障排除步骤:

检查字体可用性:确保 'Lora-SemiBold' 字体已正确安装并可用于你的 React Native 项目。你可以双重检查字体名称和是否已正确链接。

字体文件格式:验证字体文件格式是否受 React Native 支持。通常支持常见字体格式,如 .ttf(TrueType 字体)。

清除缓存:通过在项目目录中运行以下命令来清除 Metro bundler 缓存:npm start -- --reset-cache

尝试其他字体:尝试在你的应用程序中临时使用不同的字体,以查看问题是否特定于 'Lora-SemiBold' 字体,或者是否是更一般性的问题。

如果这些步骤都无法解决问题,你可能需要提供有关你项目设置的更具体的详细信息。

英文:

From what I can tell, maybe you made a mistake in your code when you are applying styles to the '<Text>' component. The proper way for styling is 'style', not 'styles'.

  • From:
    <Text styles={{fontFamily:'Lora-SemiBold', fontSize:24, color:'#FFF'}}>Lets get Started</Text>
  • To: <Text style={{ fontFamily: 'Lora-SemiBold', fontSize: 24, color: '#FFF' }}>Lets get Started</Text>

If applying the correct styling directly to the <Text> component doesn't seem to change the font, there might be a few reasons for it. Here are some troubleshooting steps you can try:

Check Font Availability: Make sure that the 'Lora-SemiBold' font is correctly installed and available for use in your React Native project. You can double-check the font name and whether it's properly linked.

Font File Format: Verify that the font file format is supported by React Native. Common font formats like .ttf (TrueType Font) are generally supported.

Clear Cache: Clear the Metro bundler cache by running the following command in your project directory: npm start -- --reset-cache

Test with Other Fonts: Try using a different font in your app temporarily to see if the issue is specific to the 'Lora-SemiBold' font or if it's a more general problem.

If none of these steps resolve the issue, you might need to provide more specific details about your project setup.

huangapple
  • 本文由 发表于 2023年8月10日 22:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定