如何为React-Native Android项目添加不同字体粗细的字体? [英] How to add fonts for different font weights for react-native android project?

查看:319
本文介绍了如何为React-Native Android项目添加不同字体粗细的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过以下方式添加自定义字体:

I've managed to add a custom font by:

  • 将* .ttf文件放入 ProjectName/android/app/src/main/assets/fonts/中,如下所示:

  • 打开Sans.ttf
  • 打开Sans_italic.ttf
  • 打开Sans_bold.ttf
  • 打开Sans_bold_italic.ttf

,并通过 fontFamily:"Open Sans"

但是我想使用一些额外的字体粗细,例如"Semi Bold","Extra Bold".我尝试将它们添加为"Open Sans_900.ttf"并设置 fontWeight:900 ,但这没有用,它以粗体显示了该字体.

But there are extra font weights I want to use like 'Semi Bold', 'Extra Bold'. I tried adding them as 'Open Sans_900.ttf' and setting fontWeight: 900 but that didn't work, it displayed bold version of the font.

是否可以添加这些额外的字体粗细?

Is there a way to add these additional font weights?

推荐答案

React Native中对Android自定义字体的开箱即用支持有些限制.除了 normal bold 之外,它不支持字体粗细(它还支持 italic fontStyle ).如果需要其他自定义权重,则必须将它们添加为具有不同名称的单独字体(如David在他的回答中提到的那样).

The out of the box support for custom fonts on Android is a little limited in React Native. It does not support font weights other than normal and bold (it also supports the italic fontStyle). If you want other custom weights, you must add them as separate fonts with a different name (as David mentioned in his answer).

RN唯一会找到的字体文件具有以下格式:

The only font files that RN will find are of the following format:

  • {fontFamilyName}
  • {fontFamilyName} _bold
  • {fontFamilyName} _italic
  • {fontFamilyName} _bold_italic

受支持的扩展名: .ttf .otf

(我所知道的)任何地方都没有对此进行记录,但是您可以在此处阅读字体管理器代码:

This really isn't documented anywhere (that I know of), but you can read the Font Manager code here: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java

这篇关于如何为React-Native Android项目添加不同字体粗细的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆