如何防止系统字体大小更改对 nativescript-angular android 应用程序的影响
how to prevent system font-size changing effects to nativescript-angular android application
问题是当我调整 android 设备系统字体大小时,我的
用户应用程序的字体大小正在更改为设备系统字体大小
如何预防
Android 中字体大小的默认单位是缩放像素,将其设置为 DIP 或 PX 将防止自动缩放。
已针对 v6.0 进行测试
import { isAndroid } from 'tns-core-modules/platform';
import { TextBase } from 'tns-core-modules/ui/text-base/text-base';
declare var android; // required if tns-platform-declarations is not installed
if (isAndroid) {
TextBase.prototype[require("tns-core-modules/ui/text-base/text-base-common").fontSizeProperty.setNative] = function (value) {
if (!this.formattedText || (typeof value !== "number")) {
if (typeof value === "number") {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, value);
}
else {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
}
}
};
}
由于您使用的是 Angular,您可以将此代码放在 main.ts
或应用程序模块中。
对于 NativeScript 7+,我选择直接修改 @nativescript/core/ui/text-base/index.android.js 并使用 patch-package 来管理更改,因为我使用 patch-package 来处理少数情况我必须自己迁移的插件。这是补丁,实际上是 one-line 更改:
--- a/node_modules/@nativescript/core/ui/text-base/index.android.js
+++ b/node_modules/@nativescript/core/ui/text-base/index.android.js
@@ -274,7 +274,10 @@ export class TextBase extends TextBaseCommon {
[fontSizeProperty.setNative](value) {
if (!this.formattedText || typeof value !== 'number') {
if (typeof value === 'number') {
- this.nativeTextViewProtected.setTextSize(value);
+ // Override font scaling so that app styling remains consistent regardless of system font size.
+ // See
+ this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, value);
+ // this.nativeTextViewProtected.setTextSize(value);
}
else {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
问题是当我调整 android 设备系统字体大小时,我的 用户应用程序的字体大小正在更改为设备系统字体大小 如何预防
Android 中字体大小的默认单位是缩放像素,将其设置为 DIP 或 PX 将防止自动缩放。
已针对 v6.0 进行测试
import { isAndroid } from 'tns-core-modules/platform';
import { TextBase } from 'tns-core-modules/ui/text-base/text-base';
declare var android; // required if tns-platform-declarations is not installed
if (isAndroid) {
TextBase.prototype[require("tns-core-modules/ui/text-base/text-base-common").fontSizeProperty.setNative] = function (value) {
if (!this.formattedText || (typeof value !== "number")) {
if (typeof value === "number") {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, value);
}
else {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
}
}
};
}
由于您使用的是 Angular,您可以将此代码放在 main.ts
或应用程序模块中。
对于 NativeScript 7+,我选择直接修改 @nativescript/core/ui/text-base/index.android.js 并使用 patch-package 来管理更改,因为我使用 patch-package 来处理少数情况我必须自己迁移的插件。这是补丁,实际上是 one-line 更改:
--- a/node_modules/@nativescript/core/ui/text-base/index.android.js
+++ b/node_modules/@nativescript/core/ui/text-base/index.android.js
@@ -274,7 +274,10 @@ export class TextBase extends TextBaseCommon {
[fontSizeProperty.setNative](value) {
if (!this.formattedText || typeof value !== 'number') {
if (typeof value === 'number') {
- this.nativeTextViewProtected.setTextSize(value);
+ // Override font scaling so that app styling remains consistent regardless of system font size.
+ // See
+ this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, value);
+ // this.nativeTextViewProtected.setTextSize(value);
}
else {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);