鸿蒙-case案例动态注册字体

鸿蒙case案例,动态改变字体样式,通过点击不同字体按钮,更改为对应的字体

1. 给文字设置fontFamily属性,动态切换字体

scss 复制代码
Text('点击改变按钮,切换不同的字体样式,快来试试')
  .fontFamily(this.currentFont)
  .fontSize(18)
  .margin({ top: 10 })
  .padding(20)
  .width("70%")
  .borderRadius(20)
  .backgroundColor('#ffefefef')

2. Progress点击加载和切换字体

Progress 进度条组件

Progress(options: ProgressOptions<Type>)

  • ProgressOptions<Type>对象
    • value 指定当前进度值,默认0 number类型

    • total 指定进度总长,默认100 number类型

    • type 指定进度条类型

      • ProgressTyp枚举
      1. Linear 线性样式
      2. Ring 环形无刻度样式
      3. Eclipse 圆形样式
      4. ScaleRing 环形有刻度样式
      5. Capsule 胶囊样式
    • style 指定进度条样式

kotlin 复制代码
Progress({ value: this.downloadNow, total: this.downloadTotal, type: ProgressType.Capsule })
  .width("40%")
  .height(40)
  // CapsuleStyleOptions
  .style({
    content: this.progressContent,  // 文本内容
    showDefaultPercentage: true,  // 显示百分比文本的开关
    borderColor: '#ff3f89fd',  // 内描边颜色
    fontColor: this.progressFontColor  // 文本颜色
  })
  .color(this.progressColor)  // 设置进度条前景色
  .backgroundColor(this.progressBgColor)  // 进度条底色
  .onClick((event: ClickEvent) => {
    this.useFont()
  })

3. useFont()函数改变字体

  • 判断文件沙箱路径是否存在,存在注册使用

fs.accessSync

以同步方法检查文件是否存在。

accessSync(path: string, mode?: AccessModeType): boolean

  • path 文件应用沙箱路径。
  • mode 文件校验的权限。

font.registerFont

在字体管理中注册自定义字体。

  • FontOptions
    • familyName 设置注册的字体名称。
    • familySrc 设置注册字体文件的路径。
ini 复制代码
const filePath = CACHE_DIR + FILE_NAME
let res = fs.accessSync(filePath)
if (res) {
  font.registerFont({
    familyName: sy,
    familySrc: FILE_HEADER + filePath
  })
  this.currentFont = sy
  return
}
  • 不存在沙箱文件,下载字体文件使用

downloadFile(context: BaseContext, config: DownloadConfig, callback: AsyncCallback<DownloadTask>): void;

context 基于应用程序的上下文。 config 下载的配置信息。 downloadTask 监听下载任务对象

kotlin 复制代码
try {
// 设置文本内容、前景色、字体颜色、背景色
  this.progressContent = loadingText
  this.progressColor = '#ffefefef'
  this.progressFontColor = '#ff182431'
  this.progressBgColor = '#ffffffff'
  // 下载沙箱文件
  request.downloadFile(getContext(), {
    url: URL,
    filePath: filePath
  }, (err, downloadTask) => {
    if (err) {
      Log.info(err, 'error info')
      return
      ...
    }
  })
} catch (err) {
  Log.info(err, `downloadFile error`)
}
  • downloadTask任务对象监听

fs.unlink

删除单个文件

unlink(path: string): Promise<void>

path 文件的应用沙箱路径

kotlin 复制代码
// 监听下载进度信息
  downloadTask.on('progress', (now, total) => {
      this.progressContent = loadingText;
      this.downloadNow = now
      this.downloadTotal = total
    })
// 监听下载任务完成
    downloadTask.on('complete', () => {
      this.progressColor = '#ff3f89fd'
      this.progressFontColor = '#ffffff'
      this.progressBgColor = '#ff3f89fd'
      this.progressContent = sy
      downloadTask.off('progress')  // 取消下载进度监听
      downloadTask.off('fail')  // 取消下载任务失败监听
      font.registerFont({
        familyName: sy,
        familySrc: FILE_HEADER + filePath
      })
      this.currentFont = sy
    })
 // 监听下载任务失败
    downloadTask.on('fail', (err: number) => {
      let res = fs.accessSync(filePath)
      if (res) {
        fs.unlink(filePath)
        downloadTask.off('fail')
      }
    })
    ```
相关推荐
熬夜敲代码的小N27 分钟前
鸿蒙PC开发者必备!GitNext深度测评:一站式Git管理工具
git·华为·harmonyos
秋の本名2 小时前
第一章 鸿蒙生态架构与开发理念
华为·wpf·harmonyos
Ww.xh2 小时前
鸿蒙系统中HTML与Vue集成方案
vue.js·html·harmonyos
前端不太难3 小时前
鸿蒙游戏 CI/CD:为什么你还在手动打包?
游戏·ci/cd·harmonyos
全栈若城3 小时前
HarmonyOS Pen Kit 实战:手写笔轻捏、双击与取色器完整集成
华为·harmonyos·手写笔·harmonyos6
xmdy58664 小时前
Flutter+开源鸿蒙实战|校园易生活Day1 项目初始化搭建+开发环境校验+工程目录规范+第三方库集成+多端屏幕适配+全局底部导航
flutter·开源·harmonyos
想你依然心痛5 小时前
HarmonyOS 6(API 23)实战:打造“空间交互式AR健身私教“——基于Face AR疲劳监测 + Body AR姿态识别的沉浸光感运动系统
ar·restful·harmonyos·悬浮导航·沉浸光感
xmdy58665 小时前
Flutter+开源鸿蒙实战|校园易生活Day2 第三方库批量集成+全局Toast提示+网络状态监听+首页轮播图+资讯卡片布局
flutter·开源·harmonyos
前端不太难6 小时前
为什么说鸿蒙 App 是“状态系统”?
华为·状态模式·harmonyos
●VON6 小时前
猫咪专注 CatFocus 技术博客:一款鸿蒙原生自律计时工具的设计与实现
学习·华为·harmonyos·von·猫咪专注