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 '@ohos.font' 17``` 18 19## font.registerFont 20 21registerFont(options: FontOptions): void 22 23Registers a custom font with the font manager. 24 25**System capability**: SystemCapability.ArkUI.ArkUI.Full 26 27**Parameters** 28 29| Name | Type | Mandatory | Description | 30| ------- | --------------------------- | ---- | ----------- | 31| options | [FontOptions](#fontoptions) | Yes | Information about the custom font to register.| 32 33## FontOptions 34 35**System capability**: SystemCapability.ArkUI.ArkUI.Full 36 37| Name | Type | Mandatory | Description | 38| ---------- | ------ | ---- | ------------ | 39| familyName | string \| [Resource](../arkui-ts/ts-types.md#resource)<sup>10+</sup> | Yes | Name of the custom font to register. | 40| familySrc | string \| [Resource](../arkui-ts/ts-types.md#resource)<sup>10+</sup> | Yes | Path of the custom font to register.| 41 42**Example** 43 44```ts 45// xxx.ets 46import font from '@ohos.font'; 47 48@Entry 49@Component 50struct FontExample { 51 @State message: string = 'Hello World' 52 53 // 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. 54 @State unicode: string = '\u0000' 55 @State codePoint: string = String.fromCharCode(0x0000) 56 57 aboutToAppear() { 58 // Both familyName and familySrc support the string type. 59 font.registerFont({ 60 familyName: 'medium', 61 familySrc: '/font/medium.ttf' // The font folder is at the same level as the pages folder. 62 }) 63 64 // Both familyName and familySrc support the Resource type. 65 font.registerFont({ 66 familyName: $r('app.string.font_name'), 67 familySrc: $r('app.string.font_src') 68 }) 69 70 // familySrc supports the $rawfile type. 71 font.registerFont({ 72 familyName: 'mediumRawFile', 73 familySrc: $rawfile('font/medium.ttf') 74 }) 75 76 // Register iconFont. 77 font.registerFont({ 78 familyName: 'iconFont', 79 familySrc: '/font/iconFont.ttf' 80 }) 81 } 82 83 build() { 84 Column() { 85 Text(this.message) 86 .align(Alignment.Center) 87 .fontSize(20) 88 .fontFamily('medium') // medium: name of the custom font to register. (Registered fonts such as $r('app.string.mediumFamilyName') and 'mediumRawFile' can also be used.) 89 90 // Two methods of using iconFont 91 Text(this.unicode) 92 .align(Alignment.Center) 93 .fontSize(20) 94 .fontFamily('iconFont') 95 Text(this.codePoint) 96 .align(Alignment.Center) 97 .fontSize(20) 98 .fontFamily('iconFont') 99 }.width('100%') 100 } 101} 102``` 103## font.getSystemFontList<sup>10+</sup> 104 105getSystemFontList(): Array\<string> 106 107Obtains the list of supported fonts. 108 109**System capability**: SystemCapability.ArkUI.ArkUI.Full 110 111**Return value** 112 113| Type | Description | 114| -------------------- | ----------------- | 115| Array\<string> | List of supported fonts. | 116 117**Example** 118 119```ts 120// xxx.ets 121import font from '@ohos.font'; 122 123@Entry 124@Component 125struct FontExample { 126 fontList: Array<string> = new Array<string>(); 127 build() { 128 Column() { 129 Button("getSystemFontList") 130 .width('60%') 131 .height('6%') 132 .onClick(()=>{ 133 this.fontList = font.getSystemFontList() 134 }) 135 }.width('100%') 136 } 137} 138``` 139 140## font.getFontByName<sup>10+</sup> 141 142getFontByName(fontName: string): FontInfo 143 144Obtains information about a system font based on the font name. 145 146**System capability**: SystemCapability.ArkUI.ArkUI.Full 147 148**Parameters** 149 150| Name | Type | Mandatory | Description | 151| ---------- | --------- | ------- | ------------ | 152| fontName | string | Yes | System font name.| 153 154**Return value** 155 156| Type | Description | 157| ---------------- | ---------------------------- | 158| FontInfo | Information about the system font. | 159 160## FontInfo<sup>10+</sup> 161 162**System capability**: SystemCapability.ArkUI.ArkUI.Full 163 164| Name | Type | Mandatory | Description | 165| -------------- | ------- | ------------------------- | ------------------------- | 166| path | string | Yes| File path of the system font. | 167| postScriptName | string | Yes| PostScript name of the system font.| 168| fullName | string | Yes| Name of the system font. | 169| family | string | Yes| Family of the system font. | 170| subfamily | string | Yes| Subfamily of the system font. | 171| weight | number | Yes| Weight of the system font. | 172| width | number | Yes| Width of the system font. | 173| italic | boolean | Yes| Whether the system font is italic. | 174| monoSpace | boolean | Yes| Whether the system font is monospaced. | 175| symbolic | boolean | Yes| Whether the system font supports symbols. | 176 177**Example** 178 179```ts 180// xxx.ets 181import font from '@ohos.font'; 182 183@Entry 184@Component 185struct FontExample { 186 fontList: Array<string> = new Array<string>(); 187 fontInfo: font.FontInfo = font.getFontByName(''); 188 build() { 189 Column() { 190 Button("getFontByName") 191 .onClick(() => { 192 this.fontInfo = font.getFontByName('Sans Italic') 193 console.log("getFontByName(): path = " + this.fontInfo.path) 194 console.log("getFontByName(): postScriptName = " + this.fontInfo.postScriptName) 195 console.log("getFontByName(): fullName = " + this.fontInfo.fullName) 196 console.log("getFontByName(): Family = " + this.fontInfo.family) 197 console.log("getFontByName(): Subfamily = " + this.fontInfo.subfamily) 198 console.log("getFontByName(): weight = " + this.fontInfo.weight) 199 console.log("getFontByName(): width = " + this.fontInfo.width) 200 console.log("getFontByName(): italic = " + this.fontInfo.italic) 201 console.log("getFontByName(): monoSpace = " + this.fontInfo.monoSpace) 202 console.log("getFontByName(): symbolic = " + this.fontInfo.symbolic) 203 }) 204 }.width('100%') 205 } 206} 207``` 208 209## font.getUIFontConfig<sup>11+</sup> 210getUIFontConfig() : UIFontConfig 211 212Obtains the UI font configuration of the system. 213 214**System capability**: SystemCapability.ArkUI.ArkUI.Full 215 216**Return value** 217| Type | Description | 218| ---------------- | ---------------------------- | 219| [UIFontConfig](#uifontconfig11) | UI font configuration of the system. | 220 221## UIFontConfig<sup>11+</sup> 222**System capability**: SystemCapability.ArkUI.ArkUI.Full 223| Name | Type | Mandatory | Description | 224| -------------- | ------- | ------------------------- | ------------------------- | 225| fontDir | Array\<string> | Yes| Path to the system font file. | 226| generic | Array\<[UIFontGenericInfo](#uifontgenericinfo11)> | Yes| List of supported generic font families.| 227| fallbackGroups | Array\<[UIFontFallbackGroupInfo](#uifontfallbackgroupinfo11)> | Yes| List of alternate generic font families. | 228 229## UIFontGenericInfo<sup>11+</sup> 230**System capability**: SystemCapability.ArkUI.ArkUI.Full 231| Name | Type | Mandatory | Description | 232| -------------- | ------- | ------------------------- | ------------------------- | 233| family | string | Yes| Font family name, which is the value of **family** specified in the font file. | 234| alias | Array\<[UIFontAliasInfo](#uifontaliasinfo11)> | Yes| Alias list.| 235| adjust | Array\<[UIFontAdjustInfo](#uifontadjustinfo11)> | No| Weight of the font when displayed, which corresponds to the original weight.| 236 237## UIFontFallbackGroupInfo<sup>11+</sup> 238**System capability**: SystemCapability.ArkUI.ArkUI.Full 239| Name | Type | Mandatory | Description | 240| -------------- | ------- | ------------------------- | ------------------------- | 241| fontSetName | string | Yes| Name of the font family corresponding to the alternate fonts. | 242| 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.| 243 244## UIFontAliasInfo<sup>11+</sup> 245**System capability**: SystemCapability.ArkUI.ArkUI.Full 246| Name | Type | Mandatory | Description | 247| -------------- | ------- | ------------------------- | ------------------------- | 248| name | string | Yes| Alias name. | 249| 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.| 250 251## UIFontAdjustInfo<sup>11+</sup> 252**System capability**: SystemCapability.ArkUI.ArkUI.Full 253| Name | Type | Mandatory | Description | 254| -------------- | ------- | ------------------------- | ------------------------- | 255| weight | number | Yes| Original weight of the font. | 256| to | number | Yes| Weight of the font displayed in the application.| 257 258## UIFontFallbackInfo<sup>11+</sup> 259**System capability**: SystemCapability.ArkUI.ArkUI.Full 260| Name | Type | Mandatory | Description | 261| -------------- | ------- | ------------------------- | ------------------------- | 262| language | string | Yes| Language supported by the font family. The language format is BCP 47. | 263| family | string | Yes| Font family name, which is the value of **family** specified in the font file.| 264 265**Example** 266 267```ts 268// xxx.ets 269import font from '@ohos.font'; 270@Entry 271@Component 272struct FontExample { 273 build() { 274 Column() { 275 Button("getUIFontConfig") 276 .width('60%') 277 .height('6%') 278 .margin(50) 279 .onClick(()=>{ 280 let fontConfig = font.getUIFontConfig(); 281 console.log("font-dir -----------" + String(fontConfig.fontDir.length)); 282 for (let i = 0; i < fontConfig.fontDir.length; i ++) { 283 console.log(fontConfig.fontDir[i]); 284 } 285 console.log("generic-------------" + String(fontConfig.generic.length)); 286 for (let i = 0; i < fontConfig.generic.length; i ++){ 287 console.log("family:" + fontConfig.generic[i].family); 288 for (let j = 0; j < fontConfig.generic[i].alias.length; j ++){ 289 console.log(fontConfig.generic[i].alias[j].name + " " + fontConfig.generic[i].alias[j].weight); 290 } 291 for (let j = 0; j < fontConfig.generic[i].adjust.length; j ++){ 292 console.log(fontConfig.generic[i].adjust[j].weight + " " + fontConfig.generic[i].adjust[j].to); 293 } 294 } 295 console.log("fallback------------" + String(fontConfig.fallbackGroups.length)); 296 for (let i = 0; i < fontConfig.fallbackGroups.length; i ++){ 297 console.log("fontSetName:" + fontConfig.fallbackGroups[i].fontSetName); 298 for (let j = 0; j < fontConfig.fallbackGroups[i].fallback.length; j ++){ 299 console.log("language:" + fontConfig.fallbackGroups[i].fallback[j].language + " family:" + fontConfig.fallbackGroups[i].fallback[j].family); 300 } 301 } 302 }) 303 }.width('100%') 304 } 305} 306``` 307