@ohos.font (Custom Font Registration)

The font module provides APIs for registering custom fonts.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import font from '@ohos.font'

font.registerFont

registerFont(options: FontOptions): void

Registers a custom font with the font manager.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
options FontOptions Yes Information about the custom font to register.

FontOptions

Name Type Mandatory Description
familyName string Yes Name of the custom font to register.
familySrc string Yes Path of the custom font to register.

Example

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

@Entry
@Component
struct FontExample {
  @State message: string =' Hello, World'

  aboutToAppear() {
    font.registerFont({
      familyName: 'medium',
      familySrc: '/font/medium.ttf'
    })
  }

  build() {
    Column() {
      Text(this.message)
        .align(Alignment.Center)
        .fontSize(20)
        .fontFamily('medium') // medium: name of the custom font to register.
        .height('100%')
    }.width('100%')
  }
}