如何在React Native for IOS应用程序中禁用字体缩放?

A-J*_*J-A 26 ios react-native

设备字体大小的放大有时会中断(样式明智).

Lev*_*evi 52

对于React Native 0.56.0+

Text.defaultProps = Text.defaultProps || {};
Text.defaultProps.allowFontScaling = false;
Run Code Online (Sandbox Code Playgroud)

  • 如果你们中的一些人想知道在哪里实现它,请将上面的代码放在`〜/ index.js`中的import语句下面.一定要将`Text`组件导入RN.顺便说一下,我正在运行RN 0.57.8. (5认同)
  • 黑客,但仍然辉煌。正在使用它。 (2认同)

A-J*_*J-A 20

对于React原生0.58+

最好保留字体缩放,但可以使用Text new prop maxFontSizeMultiplier限制它

对于React原生0.56+使用Levi的答案

Text.defaultProps = Text.defaultProps || {};
Text.defaultProps.allowFontScaling = false;
Run Code Online (Sandbox Code Playgroud)

对于React原生0.55及更低

Text.defaultProps.allowFontScaling=false在应用程序的开头添加(例如main.js或app.js等...),在整个应用程序中对所有Text组件应用此prop.

  • @AJA我刚刚从react-native 0.55.4升级到0.56.0,而Text.defaultProps是未定义的. (2认同)

小智 6

我有点晚了,但如果有人想用 Typescript 得到答案,这里就是

interface TextWithDefaultProps extends Text {
  defaultProps?: { allowFontScaling?: boolean };
}

(Text as unknown as TextWithDefaultProps).defaultProps = {
   ...((Text as unknown as TextWithDefaultProps).defaultProps || {}),
   allowFontScaling: false,
 };
Run Code Online (Sandbox Code Playgroud)


Kes*_*era 5

When user increase full font size from setting 
Run Code Online (Sandbox Code Playgroud)

设备字体大小的放大不会中断(样式明智)。

index.js file


import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';
import {Text, TextInput} from 'react-native';

AppRegistry.registerComponent(appName, () => App);

//ADD this 
if (Text.defaultProps == null) {
    Text.defaultProps = {};
    Text.defaultProps.allowFontScaling = false;
}

if (TextInput.defaultProps == null) {
    TextInput.defaultProps = {};
    TextInput.defaultProps.allowFontScaling = false;
}

<CalendarStrip
    shouldAllowFontScaling={false}
/>
Run Code Online (Sandbox Code Playgroud)

另请注意,某些组件不会遵循字体缩放设置,例如:Alert、PickerIOS、DatePickerIOS、TabBarIOS、SegmentedControlIOS,因为这些都是本机绘制的,不依赖于 Text 组件。