设备字体大小的放大有时会破坏样式。
设备字体大小的放大有时会破坏样式。
禁用字体缩放可能会损害您的应用程序的可访问性。如果您想限制使用 React Native 0.58.0 及以上版本的应用程序的缩放,请在特定Text
组件上使用maxFontSizeMultiplier
属性。
但是,如果您确实希望在整个应用程序中禁用字体缩放,则可以通过在Text
的defaultProps
中全局设置allowFontScaling
属性来实现。
您应该将这些行放置在根入口点(通常为index.js
)中,并在AppRegistry.registerComponent
之前执行。
适用于React Native 0.56.0及以上版本
Text.defaultProps = Text.defaultProps || {};
Text.defaultProps.allowFontScaling = false;
对于早期版本的React Native,您只需要第二行代码,但两行代码都不会有害。第一行代码只是为了防止Text
组件没有defaultProps
,这种情况适用于React Native 0.56.0及以上版本。
将上述代码添加到React Native应用程序的入口文件中(通常为index.js
、app.js
或main.js
),以将此属性应用于应用程序中的所有Text
组件。
此属性仅影响Text
组件,您可能希望将相同的更改应用于TextInput
,可以使用类似的代码片段:
TextInput.defaultProps = TextInput.defaultProps || {};
TextInput.defaultProps.allowFontScaling = false;
请注意,某些组件不会遵循字体缩放设置,例如:Alert
、PickerIOS
、DatePickerIOS
、TabBarIOS
和 SegmentedControlIOS
,因为它们都是本地绘制的,不依赖于 Text
组件。
~/index.js
文件中导入语句的下方。确保还要导入Text
组件到RN中。顺便说一下,我正在运行RN 0.57.8版本。 - Mix Master MikeText.defaultProps = Text.defaultProps || {}
。它可以处理两种情况。 - dphaenerText.defaultProps
是否未定义。 - levi对于 React Native 0.58+ 版本:
保持字体缩放是最好的,但你可以通过使用 Text 组件的新属性 maxFontSizeMultiplier 来限制它。
对于 React Native 0.56+ 版本,请使用 Levi 的答案。
Text.defaultProps = Text.defaultProps || {};
Text.defaultProps.allowFontScaling = false;
对于React Native 0.55及以下版本
在应用程序的开头(例如main.js或app.js等)添加Text.defaultProps.allowFontScaling=false
,以将此属性应用于整个应用程序中的所有Text组件。
When user increase full font size from setting
增大设备字体大小不会破坏(样式方面)。
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}
/>
请注意,一些组件不会遵守字体缩放设置,例如:Alert、PickerIOS、DatePickerIOS、TabBarIOS、SegmentedControlIOS,因为它们都是本地绘制的,不依赖于Text组件。
虽然我有点晚了,但如果有人需要使用Typescript的答案,这里有一个:
interface TextWithDefaultProps extends Text {
defaultProps?: { allowFontScaling?: boolean };
}
(Text as unknown as TextWithDefaultProps).defaultProps = {
...((Text as unknown as TextWithDefaultProps).defaultProps || {}),
allowFontScaling: false,
};
if (Text.defaultProps == null) {
Text.defaultProps = {};
Text.defaultProps.allowFontScaling = false;
}
index.js
的构造函数中,我保留了这段代码。它真的很有效。顺便说一句,我使用的是React Native版本0.59.9
。"Original Answer"翻译成中文是"最初的回答"。Text.defaultProps = Text.defaultProps || {} ;
Text.defaultProps.allowFontScaling = false;
这是等同于您的代码。 - A-J-A<AppText>
组件,并将其与您的预设一起使用,而不是使用原始组件,包括自己的默认设置,其中包括字体缩放为假。这样更好,因为您可以使用自己的API丰富它。<AppText id="some.translation.key" color="primary" size="l" underline italic bold/>
对于webview,我们可以使用textZoom={100}
属性来处理字体大小的变化,如果字体大小从移动设置中改变。
如果从react-native-webview
导入。
<WebView
textZoom={100}
source={}/>
Text
组件作为备份导入为ScaledText
,然后重新定义Text
,覆盖allowFontScaling
属性。export function Text(props) {
return <ScaledText {...props} allowFontScaling={false} />;
}
Text
组件,而不是内置的React Native Text
。如果你想优雅地仅在应用程序的某些部分禁用字体缩放,这也非常有用。index.js
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);
if (Text.defaultProps == null) {
Text.defaultProps = {};
Text.defaultProps.allowFontScaling = false;
}
请注意,像 Alert , PickerIOS , DatePickerIOS , TabBarIOS 和 SegmentedControlIOS 这样的一些组件不会遵守字体缩放设置,因为它们是本地绘制的,不依赖于文本组件。
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
adjustFontScale(getResources().getConfiguration());
}
private void adjustFontScale(Configuration configuration) {
configuration.fontScale = (float) 1.0;
DisplayMetrics metrics = getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
}