首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止系统字体大小的变化对nativescript角android应用程序的影响

如何防止系统字体大小的变化对nativescript角android应用程序的影响
EN

Stack Overflow用户
提问于 2019-07-24 11:30:57
回答 2查看 1.3K关注 0票数 1

问题是当我调整android设备系统的字体大小时,我的用户应用程序的字体大小正在改变为设备系统字体大小,如何防止它发生。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-25 07:51:38

Android中字体大小的默认单位是缩放像素,将其设置为DIP或PX将阻止自动缩放。

在v6.0上的测试

代码语言:javascript
复制
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或应用程序模块中。

票数 4
EN

Stack Overflow用户

发布于 2022-02-26 16:22:16

对于NativeScript 7+,我选择直接修改NativeScript,并使用修补程序包来管理更改,因为我不得不迁移自己的一些插件使用修补程序包。下面是修补程序,实际上是一行更改:

代码语言:javascript
复制
--- 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57181909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档