1# Class (Font) 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @hddgzw--> 5<!--Designer: @pssea--> 6<!--Tester: @jiaoaozihao--> 7<!--Adviser: @HelloCrease--> 8 9注册自定义字体的信息。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14> 15> - 本Class首批接口从API version 10开始支持。 16> 17> - 以下API需先使用UIContext中的[getFont()](./arkts-apis-uicontext-uicontext.md#getfont)方法获取到Font对象,再通过该对象调用对应方法。 18> 19> - 推荐使用字体引擎的[loadFontSync](../apis-arkgraphics2d/js-apis-graphics-text.md#loadfontsync)接口注册自定义字体。 20 21## registerFont 22 23registerFont(options: font.FontOptions): void 24 25在字体管理中注册自定义字体。 26 27该接口为异步接口,不支持并发调用。 28 29**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 30 31**系统能力:** SystemCapability.ArkUI.ArkUI.Full 32 33**参数:** 34 35| 参数名 | 类型 | 必填 | 说明 | 36| ------- | ---------------------------------------- | ---- | ----------- | 37| options | [font.FontOptions](js-apis-font.md#fontoptions) | 是 | 注册的自定义字体信息。<br/>**说明:**<br/>设置注册字体文件的路径,读取系统沙箱路径内的资源时,建议使用file://路径前缀的字符串,需要确保沙箱目录路径下的文件存在并且有可读权限。 | 38 39**示例:** 40 41<!--code_no_check--> 42```ts 43// xxx.ets 44import { Font } from '@kit.ArkUI'; 45 46@Entry 47@Component 48struct Index { 49 @State message: string = 'Hello World'; 50 private uiContext: UIContext = this.getUIContext(); 51 private font: Font = this.uiContext.getFont(); 52 53 aboutToAppear() { 54 this.font.registerFont({ 55 familyName: 'medium', 56 familySrc: '/font/medium.ttf' // font文件夹与pages目录同级 57 }) 58 } 59 60 build() { 61 Column() { 62 Text(this.message) 63 .align(Alignment.Center) 64 .fontSize(20) 65 .fontFamily('medium') // medium:注册自定义字体的名字($r('app.string.mediumFamilyName')、'mediumRawFile'等已注册字体也能正常使用) 66 }.width('100%') 67 } 68} 69``` 70## getSystemFontList 71 72getSystemFontList(): Array\<string> 73 74获取系统支持的字体名称列表。 75 76该接口仅在PC/2in1设备上生效,在其他设备上返回空数组。 77 78推荐使用[getSystemFontFullNamesByType](../apis-arkgraphics2d/js-apis-graphics-text.md#textgetsystemfontfullnamesbytype14)接口获取系统最新支持的字体列表数据。 79 80**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 81 82**系统能力:** SystemCapability.ArkUI.ArkUI.Full 83 84**返回值:** 85 86| 类型 | 说明 | 87| -------------- | --------- | 88| Array\<string> | 系统的字体名列表。 | 89 90 91**示例:** 92 93<!--code_no_check--> 94```ts 95// xxx.ets 96import { Font } from '@kit.ArkUI'; 97 98@Entry 99@Component 100struct Index { 101 private uiContext: UIContext = this.getUIContext(); 102 private font: Font = this.uiContext.getFont(); 103 fontList: Array<string> = new Array<string>(); 104 105 build() { 106 Column() { 107 Button("getSystemFontList") 108 .width('60%') 109 .height('6%') 110 .onClick(() => { 111 this.fontList = this.font.getSystemFontList(); 112 console.info('getSystemFontList', JSON.stringify(this.fontList)) 113 }) 114 }.width('100%') 115 } 116} 117``` 118 119## getFontByName 120 121getFontByName(fontName: string): font.FontInfo 122 123根据传入的系统字体名称获取系统字体的相关信息。 124 125**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 126 127**系统能力:** SystemCapability.ArkUI.ArkUI.Full 128 129**参数:** 130 131| 参数名 | 类型 | 必填 | 说明 | 132| -------- | ------ | ---- | ------- | 133| fontName | string | 是 | 系统的字体名。 | 134 135**返回值:** 136 137| 类型 | 说明 | 138| ----------------------------------------- | -------------- | 139| [font.FontInfo](js-apis-font.md#fontinfo10) | 字体的详细信息。 | 140 141**示例:** 142 143<!--code_no_check--> 144```ts 145// xxx.ets 146import { Font, font } from '@kit.ArkUI'; 147 148@Entry 149@Component 150struct Index { 151 private uiContext: UIContext = this.getUIContext(); 152 private font: Font = this.uiContext.getFont(); 153 fontInfo: font.FontInfo = this.font.getFontByName(''); 154 155 build() { 156 Column() { 157 Button("getFontByName") 158 .width('60%') 159 .height('6%') 160 .onClick(() => { 161 this.fontInfo = this.font.getFontByName('HarmonyOS Sans Italic'); 162 console.info("getFontByName(): path = " + this.fontInfo.path); 163 console.info("getFontByName(): postScriptName = " + this.fontInfo.postScriptName); 164 console.info("getFontByName(): fullName = " + this.fontInfo.fullName); 165 console.info("getFontByName(): family = " + this.fontInfo.family); 166 console.info("getFontByName(): subfamily = " + this.fontInfo.subfamily); 167 console.info("getFontByName(): weight = " + this.fontInfo.weight); 168 console.info("getFontByName(): width = " + this.fontInfo.width); 169 console.info("getFontByName(): italic = " + this.fontInfo.italic); 170 console.info("getFontByName(): monoSpace = " + this.fontInfo.monoSpace); 171 console.info("getFontByName(): symbolic = " + this.fontInfo.symbolic); 172 }) 173 }.width('100%') 174 } 175} 176``` 177