• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 **** is the Unicode character of the specified icon.
54  @State unicode: string = '\u****'
55  @State codePoint: string = String.fromCharCode(0x****)
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