鸿蒙总改变字体大小设置

1.UI代码

总体思路就是在设置字体大小以后存到本地,用的时候再取,app里的所有字体大小不能直接写,都必须用本地存储的模式去做,这个就是官方demo的方法,没有别的办法目前,可以写在onPageShow里不过这样太麻烦,页面多的话,我这边直接建议重启app

TypeScript 复制代码
/*
 *
 *   Copyright (c) 2025 Huawei Device Co., Ltd.
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
import { DynamicsRouter, getFontSize, Logger, PreferencesUtils } from "common";
import { ScalesClass, scalesList, TextClass, textList } from "../model/model";
import { promptAction } from "@kit.ArkUI";

@Builder
export function SliderLayoutBuilder() {
  SliderLayout()
}

@Component
export struct SliderLayout {
  @State changeFontSize: number = getFontSize(); //字体大小
  @StorageProp('topRectHeight') topRectHeight: number = 0 //获取状态栏顶部区域高度
  @StorageProp('bottomRectHeight') bottomRectHeight: number = 0 //获取状态栏底部区域高度

  //预览字体大小
  @Builder
  fontPreview() {
    Column() {
      // 标题
      Text('标题大小')
        .fontSize(this.changeFontSize + 2)
        .fontWeight(700)
        .fontColor('#000000')
        .margin({
          left: 15,
          right: 15
        })
      // 正常
      Text('你可根据阅读习惯,拖动下边的滑块,设置字体大小。设置后会改变文章的字体大小,在打开文章即可生效')
        .fontSize(this.changeFontSize)
        .fontColor('#cc000000')
        .margin({
          top: 15,
          left: 15,
          right: 15
        })
    }
    .alignItems(HorizontalAlign.Start)
  }

  //画刻度
  @Builder
  scalesDrawing() {
    Stack({ alignContent: Alignment.TopStart }) {
      ForEach(scalesList, (item: ScalesClass) => {
        Line()
          .width(item.width)
          .height(item.height)
          .startPoint([item.horizontal, item.vertical])
          .endPoint([item.horizontal, item.vertical - 8])
          .stroke('#6f000000')
      })
    }
  }

  build() {
    NavDestination() {
      Row() {
        Image($r('app.media.back'))
          .width(20)
          .height(20)
          .onClick(() => {
            DynamicsRouter.pop()
          })

        Text('字体设置')
          .fontSize(this.changeFontSize + 1)
          .fontWeight(FontWeight.Medium)
          .fontColor('#000000')
          .layoutWeight(1)
          .textAlign(TextAlign.Center)
      }
      .backgroundColor('#FFFFFF')
      .width('100%')
      .height(44)
      .padding({ left: 15, right: 15 })

      Column() {
        this.fontPreview()
        Column() {
          this.scalesDrawing()
          Column() {
            // 字体大小文字说明
            Row() {
              ForEach(textList, (item: TextClass) => {
                Text(item.name)
                  .fontSize(this.changeFontSize === item.size ? 18 : 14)
                  .fontWeight(this.changeFontSize === item.size ? 700 : FontWeight.Normal)
                  .width(item.width)
                  .textAlign(TextAlign.Center)
              })
            }
            .width(330)
            .margin({ right: 33 })

            Row() {
              // Slider用于选择字体大小
              Slider({
                value: this.changeFontSize === 22
                  ? 22 : this.changeFontSize,
                min: 14, //滑动最小值
                max: 20, //滑动最大值
                step: 2, //步长
                style: SliderStyle.OutSet
              })
                .stepColor(Color.Yellow)
                .trackColor('#22000000')
                .selectedColor('#22000000')
                .showSteps(true)
                .trackThickness(1)
                .stepSize(15)
                .blockStyle({ type: SliderBlockType.IMAGE, image: $r('app.media.block') })
                .blockSize({ width: 35, height: 35 })
                .onChange(async (value: number) => {
                  if (this.changeFontSize === 0) {
                    return;
                  }
                  this.changeFontSize = value
                  PreferencesUtils.saveNumberData('fontSizeChange', this.changeFontSize)
                  // PreferencesUtil.saveChangeFontSize(this.changeFontSize);
                  this.changeFontSize = PreferencesUtils.getIntData('fontSizeChange')
                  Logger.log('Get the value of changeFontSize: ' + this.changeFontSize)
                  promptAction.showToast({
                    message: '需要重启app,即可生效',
                    duration: 2000,
                    alignment: Alignment.Center
                  })
                })
            }
          }
        }
        .width(355)
        .margin({ bottom: 40 })
        .alignItems(HorizontalAlign.Center)

      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.SpaceBetween) // 主轴两端对齐
      .alignItems(HorizontalAlign.Center) // 水平居中
      .backgroundColor('#F5F5F5')
      .padding(20)
    }.hideTitleBar(true)
  }
}

2. model代码

TypeScript 复制代码
export const scalesList: ScalesClass[] = [
  {
    width: 200,
    height: 150,
    vertical: 193,
    horizontal: -56,
    index: 0
  },
  {
    width: 200,
    height: 150,
    vertical: 193,
    horizontal: 50,
    index: 1
  },
  {
    width: 200,
    height: 150,
    vertical: 193,
    horizontal: 150,
    index: 2
  },
  {
    width: 200,
    height: 150,
    vertical: 193,
    horizontal: 256,
    index: 3
  }
];

export interface ScalesClass {
  index: number;
  height: number;
  width: number;
  vertical: number;
  horizontal: number;
}

export const textList: TextClass[] = [
  {
    name: '小',
    width: 55,
    size: 14,
    index: 0
  },
  {
    name: '标准',
    width: 150,
    size: 16,
    index: 1
  },
  {
    name: '大',
    width: 55,
    size: 18,
    index: 2
  },
  {
    name: '特大',
    width: 150,
    size: 20,
    index: 3
  }
];

export interface TextClass {
  name: string;
  index: number;
  size: number;
  width: number;
}
相关推荐
科技风向标13 小时前
2025 随身 WIFI 行业报告:从拼参数到重体验,华为 / 格行 / 中兴技术差异化路径解析
华为
我爱学习_zwj14 小时前
【鸿蒙面试题-6】LazyForEach 懒加载
华为·harmonyos
倔强的石头10616 小时前
鸿蒙HarmonyOS应用开发者认证:抢占万物智联时代先机
华为·harmonyos
亚信安全官方账号16 小时前
亚信安全亮相鸿蒙生态大会2025 携手鸿蒙生态绘就万物智联新蓝图
华为·harmonyos
HarmonyOS小助手17 小时前
CodeGenie 的 AI 辅助调优让你问题定位效率大幅提升
harmonyos·鸿蒙·鸿蒙生态
HarmonyOS小助手19 小时前
《音频焦点管理》最佳实践:让鸿蒙应用中的每一段声音,都不被打扰
harmonyos·鸿蒙·鸿蒙生态
小小小小小星19 小时前
鸿蒙UI开发实战指南:解决ArkUI声明式布局错乱、组件不显示与事件响应异常
harmonyos·arkui
RUNNING123!19 小时前
华为eNSP防火墙综合网络结构训练.docx
运维·网络·华为·ssh
小Mei数码说19 小时前
华为Fit4:腕间助手,守护你的健康,带你开启智慧生活
华为·生活