问题是当我调整android设备系统的字体大小时,我的用户应用程序的字体大小正在改变为设备系统字体大小,如何防止它发生。
发布于 2019-07-24 23:51:38
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);
}
}
};
}
由于您使用的是角,您可以将此代码放在main.ts
或应用程序模块中。
发布于 2022-02-26 08:22:16
对于NativeScript 7+,我选择直接修改NativeScript,并使用修补程序包来管理更改,因为我不得不迁移自己的一些插件使用修补程序包。下面是修补程序,实际上是一行更改:
--- 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 https://stackoverflow.com/questions/57181909/how-to-prevent-system-font-size-changing-effects-to-nativescript-angular-android
+ 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);
https://stackoverflow.com/questions/57181909
复制