1# @ohos.font (Custom Font Registration) 2 3The **font** module provides APIs for registering custom fonts. 4 5> **NOTE** 6> 7> 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. 8> 9> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](./js-apis-arkui-UIContext.md#uicontext). 10> 11> Since API version 10, you can use the [getFont](./js-apis-arkui-UIContext.md#getfont) API in [UIContext](./js-apis-arkui-UIContext.md#uicontext) to obtain the [Font](./js-apis-arkui-UIContext.md#font) object associated with the current UI context. 12 13## Modules to Import 14 15```ts 16import { font } from '@kit.ArkUI' 17``` 18 19## font.registerFont 20 21registerFont(options: FontOptions): void 22 23Registers a custom font with the font manager. 24 25**Atomic service API**: This API can be used in atomic services since API version 11. 26 27**System capability**: SystemCapability.ArkUI.ArkUI.Full 28 29**Parameters** 30 31| Name | Type | Mandatory | Description | 32| ------- | --------------------------- | ---- | ----------- | 33| options | [FontOptions](#fontoptions) | Yes | Information about the custom font to register. | 34 35## FontOptions 36 37**Atomic service API**: This API can be used in atomic services since API version 11. 38 39**System capability**: SystemCapability.ArkUI.ArkUI.Full 40 41| Name | Type | Mandatory | Description | 42| ---------- | ------ | ---- | ------------ | 43| familyName | string\| [Resource](arkui-ts/ts-types.md#resource)<sup>10+</sup> | Yes | Name of the custom font to register. | 44| familySrc | string\| [Resource](arkui-ts/ts-types.md#resource)<sup>10+</sup> | Yes | Path of the custom font to register. | 45 46**Example** 47 48```ts 49// xxx.ets 50import { font } from '@kit.ArkUI'; 51 52@Entry 53@Component 54struct FontExample { 55 @State message: string = 'Hello World' 56 57 // iconFont example, where 0000 is the Unicode character of the specified icon. You need to obtain the Unicode character from the TTF file of the registered iconFont. 58 @State unicode: string = '\u0000' 59 @State codePoint: string = String.fromCharCode(0x0000) 60 61 aboutToAppear() { 62 // Both familyName and familySrc support the Resource type. 63 font.registerFont({ 64 familyName: $r('app.string.font_name'), 65 familySrc: $r('app.string.font_src') 66 }) 67 68 // familySrc supports the $rawfile type. 69 font.registerFont({ 70 familyName: 'mediumRawFile', 71 familySrc: $rawfile('font/medium.ttf') 72 }) 73 74 // Register iconFont. 75 font.registerFont({ 76 familyName: 'iconFont', 77 familySrc: '/font/iconFont.ttf' 78 }) 79 80 // Both familyName and familySrc support the string type. 81 font.registerFont({ 82 familyName: 'medium', 83 familySrc: '/font/medium.ttf' // The font folder is at the same level as the pages folder. 84 }) 85 } 86 87 build() { 88 Column() { 89 Text(this.message) 90 .align(Alignment.Center) 91 .fontSize(20) 92 .fontFamily('medium') // medium: name of the custom font to register. (Registered fonts such as $r('app.string.mediumFamilyName') and 'mediumRawFile' can also be used.) 93 94 // Two methods of using iconFont 95 Text(this.unicode) 96 .align(Alignment.Center) 97 .fontSize(20) 98 .fontFamily('iconFont') 99 Text(this.codePoint) 100 .align(Alignment.Center) 101 .fontSize(20) 102 .fontFamily('iconFont') 103 }.width('100%') 104 } 105} 106``` 107> **NOTE** 108> 109> To use custom fonts globally in an application, register the fonts through the [windowStage.loadContent](js-apis-window.md#loadcontent9) API in the [onWindowStageCreate](../apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate) lifecycle callback in the **EntryAbility.ets** file. 110> 111> In an HSP project, avoid using a relative path to register a custom font. For details, see <!--Del-->[<!--DelEnd-->Accessing Resources in an HSP Through $r<!--Del-->](../../quick-start/in-app-hsp.md#accessing-resources-in-an-hsp-through-r)<!--DelEnd-->. 112 113## font.getSystemFontList<sup>10+</sup> 114 115getSystemFontList(): Array\<string> 116 117Obtains the system font list. 118 119**Atomic service API**: This API can be used in atomic services since API version 11. 120 121**System capability**: SystemCapability.ArkUI.ArkUI.Full 122 123**Return value** 124 125| Type | Description | 126| -------------------- | ----------------- | 127| Array\<string> | List of supported fonts. | 128 129> **NOTE** 130> 131> This API takes effect only on 2-in-1 devices. 132 133**Example** 134 135```ts 136// xxx.ets 137import { font } from '@kit.ArkUI'; 138 139@Entry 140@Component 141struct FontExample { 142 fontList: Array<string> = new Array<string>(); 143 build() { 144 Column() { 145 Button("getSystemFontList") 146 .width('60%') 147 .height('6%') 148 .onClick(()=>{ 149 this.fontList = font.getSystemFontList() 150 }) 151 }.width('100%') 152 } 153} 154``` 155 156## font.getFontByName<sup>10+</sup> 157 158getFontByName(fontName: string): FontInfo 159 160Obtains information about a system font based on the font name. 161 162**Atomic service API**: This API can be used in atomic services since API version 11. 163 164**System capability**: SystemCapability.ArkUI.ArkUI.Full 165 166**Parameters** 167 168| Name | Type | Mandatory | Description | 169| ---------- | --------- | ------- | ------------ | 170| fontName | string | Yes | System font name. | 171 172**Return value** 173 174| Type | Description | 175| ---------------- | ---------------------------- | 176| FontInfo | Information about the system font. | 177 178## FontInfo<sup>10+</sup> 179 180**Atomic service API**: This API can be used in atomic services since API version 11. 181 182**System capability**: SystemCapability.ArkUI.ArkUI.Full 183 184| Name | Type | Mandatory | Description | 185| -------------- | ------- | ------------------------- | ------------------------- | 186| path | string | Yes | File path of the system font. | 187| postScriptName | string | Yes | PostScript name of the system font. | 188| fullName | string | Yes | Name of the system font. | 189| family | string | Yes | Family of the system font. | 190| subfamily | string | Yes | Subfamily of the system font. | 191| weight | number | Yes | Weight of the system font, in px. | 192| width | number | Yes | Width of the system font, in px. | 193| italic | boolean | Yes | Whether the system font is italic. | 194| monoSpace | boolean | Yes | Whether the system font is monospaced. | 195| symbolic | boolean | Yes | Whether the system font supports symbols. | 196 197**Example** 198 199```ts 200// xxx.ets 201import { font } from '@kit.ArkUI'; 202 203@Entry 204@Component 205struct FontExample { 206 fontList: Array<string> = new Array<string>(); 207 fontInfo: font.FontInfo = font.getFontByName(''); 208 build() { 209 Column() { 210 Button("getFontByName") 211 .onClick(() => { 212 this.fontInfo = font.getFontByName('HarmonyOS Sans Italic') 213 console.log("getFontByName(): path = " + this.fontInfo.path) 214 console.log("getFontByName(): postScriptName = " + this.fontInfo.postScriptName) 215 console.log("getFontByName(): fullName = " + this.fontInfo.fullName) 216 console.log("getFontByName(): Family = " + this.fontInfo.family) 217 console.log("getFontByName(): Subfamily = " + this.fontInfo.subfamily) 218 console.log("getFontByName(): weight = " + this.fontInfo.weight) 219 console.log("getFontByName(): width = " + this.fontInfo.width) 220 console.log("getFontByName(): italic = " + this.fontInfo.italic) 221 console.log("getFontByName(): monoSpace = " + this.fontInfo.monoSpace) 222 console.log("getFontByName(): symbolic = " + this.fontInfo.symbolic) 223 }) 224 }.width('100%') 225 } 226} 227``` 228 229## font.getUIFontConfig<sup>11+</sup> 230getUIFontConfig() : UIFontConfig 231 232Obtains the UI font configuration of the system. 233 234**Atomic service API**: This API can be used in atomic services since API version 12. 235 236**System capability**: SystemCapability.ArkUI.ArkUI.Full 237 238**Return value** 239| Type | Description | 240| ---------------- | ---------------------------- | 241| [UIFontConfig](#uifontconfig11) | UI font configuration of the system. | 242 243## UIFontConfig<sup>11+</sup> 244 245**Atomic service API**: This API can be used in atomic services since API version 12. 246 247**System capability**: SystemCapability.ArkUI.ArkUI.Full 248| Name | Type | Mandatory | Description | 249| -------------- | ------- | ------------------------- | ------------------------- | 250| fontDir | Array\<string> | Yes | Path to the system font file. | 251| generic | Array\<[UIFontGenericInfo](#uifontgenericinfo11)> | Yes | List of supported generic font families. | 252| fallbackGroups | Array\<[UIFontFallbackGroupInfo](#uifontfallbackgroupinfo11)> | Yes | List of alternate generic font families. | 253 254## UIFontGenericInfo<sup>11+</sup> 255 256**Atomic service API**: This API can be used in atomic services since API version 12. 257 258**System capability**: SystemCapability.ArkUI.ArkUI.Full 259| Name | Type | Mandatory | Description | 260| -------------- | ------- | ------------------------- | ------------------------- | 261| family | string | Yes | Font family name, which is the value of **family** specified in the font file. | 262| alias | Array\<[UIFontAliasInfo](#uifontaliasinfo11)> | Yes | Alias list. | 263| adjust | Array\<[UIFontAdjustInfo](#uifontadjustinfo11)> | Yes | Weight of the font when displayed, which corresponds to the original weight. | 264 265## UIFontFallbackGroupInfo<sup>11+</sup> 266 267**Atomic service API**: This API can be used in atomic services since API version 12. 268 269**System capability**: SystemCapability.ArkUI.ArkUI.Full 270| Name | Type | Mandatory | Description | 271| -------------- | ------- | ------------------------- | ------------------------- | 272| fontSetName | string | Yes | Name of the font family corresponding to the alternate fonts. | 273| fallback | Array\<[UIFontFallbackInfo](#uifontfallbackinfo11)> | Yes | Alternate fonts for the font family. If **fontSetName** is **""**, it indicates that the fonts can be used as alternate fonts for all font families. | 274 275## UIFontAliasInfo<sup>11+</sup> 276 277**Atomic service API**: This API can be used in atomic services since API version 12. 278 279**System capability**: SystemCapability.ArkUI.ArkUI.Full 280| Name | Type | Mandatory | Description | 281| -------------- | ------- | ------------------------- | ------------------------- | 282| name | string | Yes | Alias name. | 283| weight | number | Yes | Weight of the fonts included in the font family. If the value is greater than 0, the font family contains only the fonts with the specified weight. If the value is 0, the font family contains all fonts. | 284 285## UIFontAdjustInfo<sup>11+</sup> 286 287**Atomic service API**: This API can be used in atomic services since API version 12. 288 289**System capability**: SystemCapability.ArkUI.ArkUI.Full 290| Name | Type | Mandatory | Description | 291| -------------- | ------- | ------------------------- | ------------------------- | 292| weight | number | Yes | Original weight of the font. | 293| to | number | Yes | Weight of the font displayed in the application. | 294 295## UIFontFallbackInfo<sup>11+</sup> 296 297**Atomic service API**: This API can be used in atomic services since API version 12. 298 299**System capability**: SystemCapability.ArkUI.ArkUI.Full 300| Name | Type | Mandatory | Description | 301| -------------- | ------- | ------------------------- | ------------------------- | 302| language | string | Yes | Language supported by the font family. The language format is BCP 47. | 303| family | string | Yes | Font family name, which is the value of **family** specified in the font file. | 304 305**Example** 306 307```ts 308// xxx.ets 309import { font } from '@kit.ArkUI'; 310 311@Entry 312@Component 313struct FontExample { 314 build() { 315 Column() { 316 Button("getUIFontConfig") 317 .width('60%') 318 .height('6%') 319 .margin(50) 320 .onClick(()=>{ 321 let fontConfig = font.getUIFontConfig(); 322 console.log("font-dir -----------" + String(fontConfig.fontDir.length)); 323 for (let i = 0; i < fontConfig.fontDir.length; i ++) { 324 console.log(fontConfig.fontDir[i]); 325 } 326 console.log("generic-------------" + String(fontConfig.generic.length)); 327 for (let i = 0; i < fontConfig.generic.length; i ++){ 328 console.log("family:" + fontConfig.generic[i].family); 329 for (let j = 0; j < fontConfig.generic[i].alias.length; j ++){ 330 console.log(fontConfig.generic[i].alias[j].name + " " + fontConfig.generic[i].alias[j].weight); 331 } 332 for (let j = 0; j < fontConfig.generic[i].adjust.length; j ++){ 333 console.log(fontConfig.generic[i].adjust[j].weight + " " + fontConfig.generic[i].adjust[j].to); 334 } 335 } 336 console.log("fallback------------" + String(fontConfig.fallbackGroups.length)); 337 for (let i = 0; i < fontConfig.fallbackGroups.length; i ++){ 338 console.log("fontSetName:" + fontConfig.fallbackGroups[i].fontSetName); 339 for (let j = 0; j < fontConfig.fallbackGroups[i].fallback.length; j ++){ 340 console.log("language:" + fontConfig.fallbackGroups[i].fallback[j].language + " family:" + fontConfig.fallbackGroups[i].fallback[j].family); 341 } 342 } 343 }) 344 }.width('100%') 345 } 346} 347``` 348<!--no_check-->