【问题标题】:Custom font not applying React Native Elements Button component自定义字体未应用 React Native Elements Button 组件
【发布时间】:2018-09-18 15:46:04
【问题描述】:

无法在反应原生元素中为 Button 设置自定义字体。

我正在使用 "react-native-elements": "^0.19.1" 版本并尝试为按钮设置自定义字体。字体已经在 Text 组件的页面中工作。

<Button
        rightIcon={{ name: 'history', type: 'font-awesome'}}
        transparent={true}
        title='History'
        fontSize={18}
        fontWeight='bold'
        fontFamily='montserrat'
      />

但是字体没有改变或没有抛出任何错误。我也尝试给 fontFamily “textStyle”、“titleStyle” 道具。没有运气。

字体包含 sn-p,

async componentWillMount() {
try{
  await Expo.Font.loadAsync({
    montserrat: require("./assets/fonts/Montserrat.ttf"),
    montserrat_light: require("./assets/fonts/Montserrat_light.ttf")
  });
  this.setState({ loading: false });
}catch(error){
  console.warn('Error loading fonts', error);
}

}

注意:自定义字体是使用 componentWillMount 方法加载的。

提前致谢。

【问题讨论】:

  • 显示添加字体的代码,rnpm 不起作用
  • 已更新,请检查
  • 你可以试试添加为textStyle={{fontFamily: 'montserrat'}}
  • 您是否尝试过使用 style 属性将字体加载到普通的 &lt;Text&gt; 组件中?
  • 你可以尝试在按钮道具中使用 titleStyle={{ fontSize: 18 }} 吗?

标签: reactjs react-native


【解决方案1】:

最后,我找到了解决方案。问题是给按钮的fontWeight。我们必须在 Async 字体加载中定义 Bold 字体并在 fontFamily 中调用名称。

以下脚本将解决我的问题,

<Button
        rightIcon={{ name: 'history', type: 'font-awesome'}}
        transparent={true}
        title='History'
        fontSize={18}
        fontFamily='montserrat_bold'
      />

我们需要在 Async 中定义字体路径,如下所示,

async componentWillMount() {
try{
  await Expo.Font.loadAsync({
    montserrat: require("./assets/fonts/Montserrat.ttf"),
    montserrat_light: require("./assets/fonts/Montserrat_light.ttf"),
    montserrat_bold: require("./assets/fonts/Montserrat-Medium.ttf")
  });
  this.setState({ loading: false });
}catch(error){
  console.warn('Error loading fints', error);
}

}

【讨论】:

  • 我做了同样的事情但仍然没有解决,即使 fontSize 不起作用
猜你喜欢
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多