在开发react-native
App时,相信你们都应该遇到过这样的问题:用户设置了系统的字体大小以后,致使本身的APP布局紊乱,甚至有些内容会被切掉/隐藏,这对于用户来说,是很是很差的用户体验。javascript
那为何会出现这种状况呢呢?缘由是咱们在开发的时候,布局的前提是系统的字体大小设置为默认大小,因此只能保证在系统字体大小正常的状况下,咱们的布局是友好的,html
那么,咱们应该如何解决这个问题呢?今天这篇文章,就给你们介绍几种解决方案。java
在react-native
中用来显示文字的,通常会用到两个组件:Text
和TextInput
。因此,咱们只要针对这两个组件作好工做,那就基本上解决了字体大小适配的问题react
Text
和TextInput
它们有一个共同属性:ios
allowFontScaling
git
这个属性在react-native
官方文档中解释以下:es6
Specifies whether fonts should scale to respect Text Size accessibility settings. The default is
true
.github
意思是:redux
是否随系统指定的文字大小变化而变化。默认值为
true
react-native
这给我提供了解决方案,只要咱们给Text
和TextInput
的属性allowFontScaling
设置值为false
,那么文字大小就不会随系统字体大小的变化而变化。
咱们有几种方式来设置Text
和TextInput
的allowFontScaling
。第一种:
Text
和TextInput
组件设置allowFontScaling = false
<Text allowFontScaling={false}/>
<TextInput allowFontScaling={false}/>
复制代码
这种方案效率很低,须要在每一个使用到这两个组件的地方都加上这个属性。可是通常这两个组件的使用率仍是很高的,因此这是一个庞大的工做量,并且在开发过程中,咱们也很容易忘记设置它
那么有没有更好实现方式呢?固然有,这就是下面讲的第二种:
咱们能够自定义一个组件MyText
, 而后统一设置allowFontScaling = false
属性,而后在其余须要调用的时候,直接用MyText
代替Text
。
MyText.js
import React from 'react'
import {Text} from 'react-native'
export default class MyText extends React.Component {
render() {
return (
<Text
allowFontScaling={false}
{...this.props}>
{this.props.children}
</Text>
)
}
}
复制代码
这个方案足够好了,可是,你仍然可能在某段代码里,忘记使用MyText
而是直接使用Text
,这个时候,问题依然会出现。
那么,就没有一种万无一失的方案吗?固然有啦,第三种:
是的,咱们能够重写Text
的render()
方法,让Text
在渲染的时候,设置allowFontScaling = false
。这里,咱们须要用到lodash
的wrap()
方法:
0.56(不包括)版本以前
Text.prototype.render = _.wrap(Text.prototype.render, function (func, ...args) {
let originText = func.apply(this, args)
return React.cloneElement(originText, {allowFontScaling: false})
})
复制代码
注意1: 在react-native
版本0.56
以前,Text
是经过React的createReactClass
方式来建立类的,也就是说,是经过javascript
的prototype
的方式来建立类。因此重写render
方法时,须要经过Text.prototype.render
来引用
而在0.56
版本,Text
改成了es6
中extends
的实现方式来建立类,因此,须要以下方式来重写render
:
0.56(包括)版本以后
Text.render = _.wrap(Text.render, function (func, ...args) {
let originText = func.apply(this, args)
return React.cloneElement(originText, {allowFontScaling: false})
})
复制代码
你们能够查看源码,或者查看0.56
的change-log
注意2: 这段代码最好放在你app整个组件执行调用以前,好比在个人项目中,我放的位置:
import React from 'react'
import {AppRegistry, Text, DeviceEventEmitter, YellowBox} from 'react-native'
import {Provider} from 'react-redux'
import App from './src/App'
import _ from 'lodash'
//字体不随系统字体变化
Text.render = _.wrap(Text.render, function (func, ...args) {
let originText = func.apply(this, args)
return React.cloneElement(originText, {allowFontScaling: false})
})
...
...
class MyApp extends React.Component {
render() {
return (
<Provider store={store}>
<App/>
</Provider>
)
}
}
AppRegistry.registerComponent("xxx", () => MyApp);
复制代码
注意3: 可是很遗憾的是,这个只适用于Text
,TextInput
不能用于此方案。
那么,有没有一种方案,可以同时兼容Text
和TextInput
而且作到一劳永逸呢?固然有了,终极方案:
首先咱们来看各类组件的源码.
...
getDefaultProps(): Object {
return {
allowFontScaling: true,
underlineColorAndroid: 'transparent',
};
},
...
复制代码
...
static defaultProps = {
accessible: true,
allowFontScaling: true,
ellipsizeMode: 'tail',
};
...
复制代码
经过这两个代码片断能够知道,在定义Text
和TextInput
时,都有给组件设置默认属性的操做.
因此咱们能够:
TextInput.defaultProps = Object.assign({}, TextInput.defaultProps, {defaultProps: false})
Text.defaultProps = Object.assign({}, Text.defaultProps, {allowFontScaling: false})
复制代码
来直接设置Text
和TextInput
的allowFontScaling
属性默认值为false
,真正实现了一劳永逸。
经过设置defaultProps
的方式来修改allowFontScaling
的值为false
,会有一个问题。
你们在使用react-native
时,最经常使用到的navigator
应该是react-navigation。你须要单独设置headertitleallowfontscaling和allowFontScaling来确保react-navigation的tabTitle
和headerTitle
没有问题。
好了,到此,咱们就完美解决了 react-native
开发中,字体大小不随系统字体大小变化而变化 的问题。
咱们总结一下:
react-native
中使用Text
和TextInput
负责显示文字信息Text
和TextInput
中设置allowFontScaling=false
可让字体大小不随系统设置而变化render()
、设置defaultProps
默认值这四种方式来设置allowFontScaling
的值为false
render()
、设置defaultProps
默认值这两种方式,须要把设置代码放在app组件初始化以前。react-navigation
兼容