react-native 全局屏蔽系統(tǒng)大字體

Android:

修改 MainActivity.java 文件

import android.content.res.Configuration;
import android.content.res.Resources;
public class MainActivity extends ReactActivity {
...
// 禁止字體縮放
@Override
public Resources getResources() {
Resources res = super.getResources();
Configuration config=new Configuration();
config.setToDefaults();
res.updateConfiguration(config,res.getDisplayMetrics());
return res;
}
}

iOS

使用Text 的一個(gè)屬性 allowFontScaling={false}

但需要每個(gè)Text都要寫一個(gè)這個(gè)屬性,很麻煩,在不做封裝的情況下可以使用下面方法做全局設(shè)置:

在項(xiàng)目寫global變量的地方加入:
import { Text, TextInput } from 'react-native'
Text.defaultProps.allowFontScaling=false;
TextInput.defaultProps.allowFontScaling=false;
TextInput.defaultProps = Object.assign({}, TextInput.defaultProps, {allowFontScaling: false}); // 新版RN使用該方法替代
Text.defaultProps = Object.assign({}, Text.defaultProps, {allowFontScaling: false});

上面修改了全局的Text 、TextInput的allowFontScaling默認(rèn)值,如果項(xiàng)目使用了react-navigation,還需要修改 headerTitleAllowFontScaling = false ,參考API
如果引入了react-native-flux-router ,在Tab 節(jié)點(diǎn),加入allowFontScaling={false} 屬性,屏蔽掉導(dǎo)航欄和TabBar上的字體變大
<Tabs key="root" allowFontScaling={false} ......>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容