harmony 鸿蒙@ohos.font (注册自定义字体)

2023-06-24 浏览 (2076)

@ohos.font (注册自定义字体)

本模块提供注册自定义字体。

说明

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见UIContext说明。

从API version 10开始,可以通过使用UIContext中的getFont方法获取当前UI上下文关联的Font对象。

导入模块

import font from '@ohos.font'

font.registerFont

registerFont(options: FontOptions): void

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

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
optionsFontOptions注册的自定义字体信息。

FontOptions

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
familyNamestring|Resource10+设置注册的字体名称。
familySrcstring|Resource10+设置注册字体文件的路径。

示例:

// xxx.ets
import font from '@ohos.font';

@Entry
@Component
struct FontExample {
  @State message: string = '你好,世界'

  aboutToAppear() {
    // familyName和familySrc都支持string
    font.registerFont({
      familyName: 'medium',
      familySrc: '/font/medium.ttf' // font文件与pages目录同级
    })

    // familyName和familySrc都支持系统Resource
    font.registerFont({
      familyName: $r('app.string.mediumFamilyName'),
      familySrc: $r('app.string.mediumFamilySrc')
    })

    // familySrc支持RawFile
    font.registerFont({
      familyName: 'mediumRawFile',
      familySrc: $rawfile('font/medium.ttf')
    })
  }

  build() {
    Column() {
      Text(this.message)
        .align(Alignment.Center)
        .fontSize(20)
        .fontFamily('medium') // medium:注册自定义字体的名字($r('app.string.mediumFamilyName')、'mediumRawFile'等已注册字体也能正常使用)
        .height('100%')
    }.width('100%')
  }
}

font.getSystemFontList10+

getSystemFontList(): Array<string>

获取系统支持的字体名称列表。

系统能力: SystemCapability.ArkUI.ArkUI.Full

返回值:

类型说明
Array<string>系统的字体名列表。

示例:

// xxx.ets
import font from '@ohos.font';

@Entry
@Component
struct FontExample {
  fontList: Array<string> = new Array<string>();
  build() {
    Column() {
      Button("getSystemFontList")
        .width('60%')
        .height('6%')
        .onClick(()=>{
          this.fontList = font.getSystemFontList()
        })
    }.width('100%')
  }
}

font.getFontByName10+

getFontByName(fontName: string): FontInfo;

根据传入的系统字体名称获取系统字体的相关信息。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
fontNamestring系统的字体名。

返回值:

类型说明
FontInfo字体的详细信息

FontInfo10+

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
pathstring系统字体的文件路径。
postScriptNamestring系统字体的postScript名称。
fullNamestring系统字体的名称。
familystring系统字体的字体家族。
subfamilystring系统字体的子字体家族。
weightnumber系统字体的粗细程度。
widthnumber系统字体的宽窄风格属性。
italicboolean系统字体是否倾斜。
monoSpaceboolean系统字体是否紧凑。
symbolicboolean系统字体是否支持符号字体。

示例:

// xxx.ets
import font from '@ohos.font';

@Entry
@Component
struct FontExample {
  fontList: Array<string> = new Array<string>();
  fontInfo: font.FontInfo = font.getFontByName('');
  build() {
    Column() {
      Button("getFontByName")
        .onClick(() => {
          this.fontInfo = font.getFontByName('Sans Italic')
          console.log("getFontByName(): path = " + this.fontInfo.path)
          console.log("getFontByName(): postScriptName = " + this.fontInfo.postScriptName)
          console.log("getFontByName(): fullName = " + this.fontInfo.fullName)
          console.log("getFontByName(): Family = " + this.fontInfo.family)
          console.log("getFontByName(): Subfamily = " + this.fontInfo.subfamily)
          console.log("getFontByName(): weight = " + this.fontInfo.weight)
          console.log("getFontByName(): width = " + this.fontInfo.width)
          console.log("getFontByName(): italic = " + this.fontInfo.italic)
          console.log("getFontByName(): monoSpace = " + this.fontInfo.monoSpace)
          console.log("getFontByName(): symbolic = " + this.fontInfo.symbolic)
        })
    }.width('100%')
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/c97031e1c40c4c5ba0b7bf0bcdf2335c