【问题标题】:How to use a custom font for <TabBarIOS> in React Native?如何在 React Native 中为 <TabBarIOS> 使用自定义字体?
【发布时间】:2016-06-09 15:50:06
【问题描述】:

我正在使用&lt;TabBarIOS&gt; 组件,我有自定义图标工作(使用react-native-vector-icons)但是我正在努力在&lt;Icon.TabBarItemIOS&gt; 组件上获得自定义fontFamily。我希望能够更改每个图标下方文本标签的字体。

我尝试将样式添加到标签栏和标签栏项目,但是它返回错误:

2016-06-09 17:45:52.449 [warn][tid:com.facebook.react.JavaScript] Warning: Failed propType: Invalid props.style key `fontFamily` supplied to `RCTTabBar`.
Bad object: {
  "flex": 1,
  "fontFamily": "Roboto-Regular"
}
Valid keys: [
  "width",
  "height",
  "top",
  "left",
  "right",
  "bottom",
  "margin",
  "marginVertical",
  "marginHorizontal",
  "marginTop",
  "marginBottom",
  "marginLeft",
  "marginRight",
  "padding",
  "paddingVertical",
  "paddingHorizontal",
  "paddingTop",
  "paddingBottom",
  "paddingLeft",
  "paddingRight",
  "borderWidth",
  "borderTopWidth",
  "borderRightWidth",
  "borderBottomWidth",
  "borderLeftWidth",
  "position",
  "flexDirection",
  "flexWrap",
  "justifyContent",
  "alignItems",
  "alignSelf",
  "flex",
  "shadowColor",
  "shadowOffset",
  "shadowOpacity",
  "shadowRadius",
  "transform",
  "transformMatrix",
  "decomposedMatrix",
  "scaleX",
  "scaleY",
  "rotation",
  "translateX",
  "translateY",
  "backfaceVisibility",
  "backgroundColor",
  "borderColor",
  "borderTopColor",
  "borderRightColor",
  "borderBottomColor",
  "borderLeftColor",
  "borderRadius",
  "borderTopLeftRadius",
  "borderTopRightRadius",
  "borderBottomLeftRadius",
  "borderBottomRightRadius",
  "borderStyle",
  "opacity",
  "overflow",
  "elevation"
] Check the render method of `TabBarIOS`.

我浏览了文档,但找不到关于该主题的任何内容,有什么想法吗?

【问题讨论】:

  • 根据我的经验,TabBarIOS 在objective-c 之外不是很可定制。我最终使用了github.com/exponentjs/react-native-tab-navigator
  • 您能分享一下您是如何让 react-native-vector-icons 在 TabBarIOS 中工作的吗?
  • @SergeySinkovskiy,当然!它在他们的文档中:github.com/oblador/…
  • @BradBumbalough 错过了你的评论,但这就是我最终要做的。

标签: ios react-native font-family


【解决方案1】:

如果您想调整字体,则需要修改 Objective-C 桥接器。例如,在RCTTabBarItem.m 中,您有以下设置代码。您可以使用setTitleTextAttributes 指定您的字体。

- (void)setIcon:(UIImage *)icon
{
  _icon = icon;
  if (_icon && _systemIcon != NSNotFound) {
    _systemIcon = NSNotFound;
    UITabBarItem *oldItem = _barItem;
    _barItem = [UITabBarItem new];
    _barItem.title = oldItem.title;
    _barItem.imageInsets = oldItem.imageInsets;
    _barItem.selectedImage = oldItem.selectedImage;
    _barItem.badgeValue = oldItem.badgeValue;
  }

  /* set custom font for tabBarItem */
  [_barItem setTitleTextAttributes:@{
      NSFontAttributeName: [UIFont fontWithName:@"Raleway-Bold" size:10.0f]
    } forState:UIControlStateSelected];
  [_barItem setTitleTextAttributes:@{
      NSFontAttributeName: [UIFont fontWithName:@"Raleway-Bold" size:10.0f]
    } forState:UIControlStateNormal];

  if (_renderAsOriginal) {
    self.barItem.image = [_icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  } else {
    self.barItem.image = _icon;
  }
}

【讨论】:

  • 我不想将整个包添加到我的项目中只是为了改变字体,这就像一个魅力......干杯!!!
猜你喜欢
  • 2016-09-25
  • 2015-06-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多