设置React Native自定义字体
IOS的问题比较少,直接使用客户端同事给出的字体名称就可以直接使用 { fontFamily: 'customFont' }
这个字体名称是他们在外层客户端里面已经打包好了的,我可以直接通过名称调用 自己打包字体教程如下adding-custom-fonts-to-react-native 比较棘手的是安卓的字体使用,他们的字体是放在assets目录下的 /** * Class responsible to load and cache Typeface objects. It will first try to load typefaces inside * the assets/fonts folder and if it doesn't find the right Typeface in that folder will fall back * on the best matching system Typeface The supported custom fonts extensions are .ttf and .otf. For * each font family the bold,italic and bold_italic variants are supported. Given a "family" font * family the files in the assets/fonts folder need to be family.ttf(.otf) family_bold.ttf(.otf) * family_italic.ttf(.otf) and family_bold_italic.ttf(.otf) */
原来要把字体文件放在assets/fonts文件夹下才能生效 { fontFamily: 'customFont' }
试了一下,成功了! 自定义字体引用的问题解决了,另外一个比较棘手的问题是之前的项目里面Text的样式有很多,而且分布在不同的地方 // _.wrap是lodash的一个函数,用来包裹传入的函数,然后返回一个新的函数 Text.prototype.render = _.wrap(Text.prototype.render,function (func,...args) { let originText = func.apply(this,args); return cloneElement(originText,{ style: [ originText.props.style,styles.defaultFontFamily ] }); }); 这样果然成功了,其他的的代码一行不用动! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |