如何加载和使用自定义字体
2024-12-18 15:44:27
396次阅读
0个评论
字体管理中注册自定义字体。 设置对应文本的fontFamily。可参考如下代码:
// xxx.ets
import { font } from '@kit.ArkUI';
@Entry
@Component
struct FontExample {
@State message: string = 'Hello World';
aboutToAppear() {
// 注册黑色字体
font.registerFont({
familyName: 'Condensed_Black', // 注册的字体名称
familySrc: '/font/Sans_Condensed_Black.ttf' // font文件夹与pages目录同级
})
// 注册黑色斜字体
font.registerFont({
familyName: 'Condensed_Black_Italic', // 注册的字体名称
familySrc: '/font/Sans_Condensed_Black_Italic.ttf' // font文件夹与pages目录同级
})
}
build() {
Column() {
Text(this.message)
.align(Alignment.Center)
.fontSize(50)
.fontFamily('Condensed_Black') // 使用黑色字体
Text(this.message)
.align(Alignment.Center)
.fontSize(50)
.fontFamily('Condensed_Black_Italic') // 使用黑色斜字体
Text(this.message)
.align(Alignment.Center)
.fontSize(50)
}
.width('100%')
.margin({ top: 30 })
}
}
效果如图所示:

00
- 1回答
- 0粉丝
- 0关注
