HarmonyOS 如何应用手机中应用的沙箱目录的字体?

如题:HarmonyOS 如何应用手机中应用的沙箱目录的字体?

阅读 378
1 个回答

参考如下demo:

import { font } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  aboutToAppear(): void{
    try {
      let dest = getContext().filesDir + '/iconfont.ttf';
      font.registerFont({
        familyName: 'custom',
        familySrc: 'file://' + dest,
      })
    } catch (e){
      console.error('error' + JSON.stringify(e))
    }
  }
  build() {
    Row() {
      Column() {
        Text('自定义字体')
          .fontSize(50)
          .fontFamily('custom')
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}