• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.font (注册自定义字体)
2
3本模块提供注册自定义字体。
4
5> **说明**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](./js-apis-arkui-UIContext.md#uicontext)说明。
10>
11> 从API version 10开始,可以通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getFont](./js-apis-arkui-UIContext.md#getfont)方法获取当前UI上下文关联的[Font](./js-apis-arkui-UIContext.md#font)对象。
12
13## 导入模块
14
15```ts
16import { font } from '@kit.ArkUI'
17```
18
19## font.registerFont
20
21registerFont(options: FontOptions): void
22
23在字体管理中注册自定义字体。
24
25**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
26
27**系统能力:** SystemCapability.ArkUI.ArkUI.Full
28
29**参数:**
30
31| 参数名     | 类型                          | 必填   | 说明          |
32| ------- | --------------------------- | ---- | ----------- |
33| options | [FontOptions](#fontoptions) | 是    | 注册的自定义字体信息。 |
34
35## FontOptions
36
37**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
38
39**系统能力:** SystemCapability.ArkUI.ArkUI.Full
40
41| 名称         | 类型     | 必填   | 说明           |
42| ---------- | ------ | ---- | ------------ |
43| familyName | string\| [Resource](arkui-ts/ts-types.md#resource)<sup>10+</sup> | 是    | 设置注册的字体名称。   |
44| familySrc  | string\| [Resource](arkui-ts/ts-types.md#resource)<sup>10+</sup> | 是    | 设置注册字体文件的路径。 |
45
46**示例:**
47
48> **说明**
49>
50> 推荐通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getFont](./js-apis-arkui-UIContext.md#getfont)方法获取当前UI上下文关联的[Font](./js-apis-arkui-UIContext.md#font)对象。
51
52```ts
53// xxx.ets
54import { font } from '@kit.ArkUI';
55
56@Entry
57@Component
58struct FontExample {
59  @State message: string = 'Hello World'
60
61  // iconFont示例,假设0000为指定icon的Unicode,实际需要开发者从注册的iconFont的ttf文件里面获取Unicode
62  @State unicode: string = '\u0000'
63  @State codePoint: string = String.fromCharCode(0x0000)
64
65  aboutToAppear() {
66    // familyName和familySrc都支持系统Resource
67    font.registerFont({ // 建议使用 this.getUIContext().getFont().registerFont()接口
68      familyName: $r('app.string.font_name'),
69      familySrc: $r('app.string.font_src')
70    })
71
72    // familySrc支持RawFile
73    font.registerFont({
74      familyName: 'mediumRawFile',
75      familySrc: $rawfile('font/medium.ttf')
76    })
77
78    // 注册iconFont
79    font.registerFont({
80      familyName: 'iconFont',
81      familySrc: '/font/iconFont.ttf'
82    })
83
84    // familyName和familySrc都支持string
85    font.registerFont({
86      familyName: 'medium',
87      familySrc: '/font/medium.ttf' // font文件夹与pages目录同级
88    })
89  }
90
91  build() {
92    Column() {
93      Text(this.message)
94        .align(Alignment.Center)
95        .fontSize(20)
96        .fontFamily('medium') // medium:注册自定义字体的名字($r('app.string.mediumFamilyName')、'mediumRawFile'等已注册字体也能正常使用)
97
98      // 使用iconFont的两种方式
99      Text(this.unicode)
100        .align(Alignment.Center)
101        .fontSize(20)
102        .fontFamily('iconFont')
103      Text(this.codePoint)
104        .align(Alignment.Center)
105        .fontSize(20)
106        .fontFamily('iconFont')
107    }.width('100%')
108  }
109}
110```
111> **说明:**
112>
113> 应用若需全局使用自定义字体,请在EntryAbility.ets文件的[onWindowStageCreate](../apis-ability-kit/js-apis-app-ability-uiAbility.md#uiabilityonwindowstagecreate)生命周期中,通过[windowStage.loadContent](js-apis-window.md#loadcontent9)回调注册。
114>
115> 在HSP工程中,不推荐采用相对路径的方式注册自定义字体,详见[HSP资源引用](../../quick-start/in-app-hsp.md#通过$r访问hsp中的资源)。
116
117## font.getSystemFontList<sup>10+</sup>
118
119getSystemFontList(): Array\<string>
120
121获取风格字体列表。
122
123**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
124
125**系统能力:** SystemCapability.ArkUI.ArkUI.Full
126
127**返回值:**
128
129| 类型                 | 说明               |
130| -------------------- | ----------------- |
131| Array\<string>       | 系统的字体名列表。  |
132
133>  **说明:**
134>
135>  该接口仅在2in1设备上生效。
136
137**示例:**
138
139> **说明**
140>
141> 推荐通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getFont](./js-apis-arkui-UIContext.md#getfont)方法获取当前UI上下文关联的[Font](./js-apis-arkui-UIContext.md#font)对象。
142
143```ts
144// xxx.ets
145import { font } from '@kit.ArkUI';
146
147@Entry
148@Component
149struct FontExample {
150  fontList: Array<string> = new Array<string>();
151  build() {
152    Column() {
153      Button("getSystemFontList")
154        .width('60%')
155        .height('6%')
156        .onClick(()=>{
157          this.fontList = font.getSystemFontList() // 建议使用 this.getUIContext().getFont().getSystemFontList()接口
158        })
159    }.width('100%')
160  }
161}
162```
163
164## font.getFontByName<sup>10+</sup>
165
166getFontByName(fontName: string): FontInfo
167
168根据传入的系统字体名称获取系统字体的相关信息。
169
170**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
171
172**系统能力:** SystemCapability.ArkUI.ArkUI.Full
173
174**参数:**
175
176| 参数名      | 类型      | 必填    | 说明          |
177| ---------- | --------- | ------- | ------------ |
178| fontName   | string    | 是      | 系统的字体名。 |
179
180**返回值:**
181
182| 类型             | 说明                          |
183| ---------------- | ---------------------------- |
184| FontInfo         | 字体的详细信息。     |
185
186## FontInfo<sup>10+</sup>
187
188**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
189
190**系统能力:** SystemCapability.ArkUI.ArkUI.Full
191
192| 名称            | 类型    | 必填  | 说明                       |
193| -------------- | ------- | ------------------------- | ------------------------- |
194| path           | string  | 是 | 系统字体的文件路径。        |
195| postScriptName | string  | 是 | 系统字体的postScript名称。 |
196| fullName       | string  | 是 | 系统字体的名称。           |
197| family         | string  | 是 | 系统字体的字体家族。       |
198| subfamily      | string  | 是 | 系统字体的子字体家族。      |
199| weight         | number  | 是 | 系统字体的粗细程度,单位px。        |
200| width          | number  | 是 | 系统字体的宽窄风格属性,单位px。    |
201| italic         | boolean | 是 | 系统字体是否倾斜。          |
202| monoSpace      | boolean | 是 | 系统字体是否紧凑。         |
203| symbolic       | boolean | 是 | 系统字体是否支持符号字体。  |
204
205**示例:**
206
207> **说明**
208>
209> 推荐通过使用[UIContext](./js-apis-arkui-UIContext.md#uicontext)中的[getFont](./js-apis-arkui-UIContext.md#getfont)方法获取当前UI上下文关联的[Font](./js-apis-arkui-UIContext.md#font)对象。
210
211```ts
212// xxx.ets
213import { font } from '@kit.ArkUI';
214
215@Entry
216@Component
217struct FontExample {
218  fontList: Array<string> = new Array<string>();
219  fontInfo: font.FontInfo = font.getFontByName('');
220  build() {
221    Column() {
222      Button("getFontByName")
223        .onClick(() => {
224          this.fontInfo = font.getFontByName('HarmonyOS Sans Italic') // 建议使用 this.getUIContext().getFont().getFontByName()接口
225          console.log("getFontByName(): path = " + this.fontInfo.path)
226          console.log("getFontByName(): postScriptName = " + this.fontInfo.postScriptName)
227          console.log("getFontByName(): fullName = " + this.fontInfo.fullName)
228          console.log("getFontByName(): Family = " + this.fontInfo.family)
229          console.log("getFontByName(): Subfamily = " + this.fontInfo.subfamily)
230          console.log("getFontByName(): weight = " + this.fontInfo.weight)
231          console.log("getFontByName(): width = " + this.fontInfo.width)
232          console.log("getFontByName(): italic = " + this.fontInfo.italic)
233          console.log("getFontByName(): monoSpace = " + this.fontInfo.monoSpace)
234          console.log("getFontByName(): symbolic = " + this.fontInfo.symbolic)
235        })
236    }.width('100%')
237  }
238}
239```
240
241## font.getUIFontConfig<sup>11+</sup>
242getUIFontConfig() : UIFontConfig
243
244获取系统的UI字体配置。
245
246**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
247
248**系统能力:** SystemCapability.ArkUI.ArkUI.Full
249
250**返回值:**
251| 类型             | 说明                          |
252| ---------------- | ---------------------------- |
253| [UIFontConfig](#uifontconfig11)     | 系统的UI字体配置信息。          |
254
255## UIFontConfig<sup>11+</sup>
256
257**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
258
259**系统能力:** SystemCapability.ArkUI.ArkUI.Full
260| 名称            | 类型    | 必填  | 说明                       |
261| -------------- | ------- | ------------------------- | ------------------------- |
262| fontDir        | Array\<string>  | 是 | 系统字体文件所在的路径。      |
263| generic | Array\<[UIFontGenericInfo](#uifontgenericinfo11)>  | 是 | 系统所支持的通用字体集列表。 |
264| fallbackGroups       | Array\<[UIFontFallbackGroupInfo](#uifontfallbackgroupinfo11)>  | 是 | 备用字体集。           |
265
266## UIFontGenericInfo<sup>11+</sup>
267
268**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
269
270**系统能力:** SystemCapability.ArkUI.ArkUI.Full
271| 名称            | 类型    | 必填  | 说明                       |
272| -------------- | ------- | ------------------------- | ------------------------- |
273| family        | string | 是 | 字体集名,字体文件中指定的"family"值。      |
274| alias        | Array\<[UIFontAliasInfo](#uifontaliasinfo11)>  | 是 | 别名列表。 |
275| adjust       | Array\<[UIFontAdjustInfo](#uifontadjustinfo11)>  | 是 | 字体原本的weight值对应需显示的值。 |
276
277## UIFontFallbackGroupInfo<sup>11+</sup>
278
279**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
280
281**系统能力:** SystemCapability.ArkUI.ArkUI.Full
282| 名称            | 类型    | 必填  | 说明                       |
283| -------------- | ------- | ------------------------- | ------------------------- |
284| fontSetName  | string | 是 | 备用字体集所对应的字体集名称。      |
285| fallback        | Array\<[UIFontFallbackInfo](#uifontfallbackinfo11)>  | 是 | 表示以下列表为该字体集的备用字体,如果fontSetName为"",表示可以作为所有字体集的备用字体。 |
286
287## UIFontAliasInfo<sup>11+</sup>
288
289**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
290
291**系统能力:** SystemCapability.ArkUI.ArkUI.Full
292| 名称            | 类型    | 必填  | 说明                       |
293| -------------- | ------- | ------------------------- | ------------------------- |
294| name          | string  | 是 | 别名名称。      |
295| weight        | number  | 是 | 当weight>0时表示此字体集只包含所指定weight的字体,当weight=0时,表示此字体集包含所有字体。 |
296
297## UIFontAdjustInfo<sup>11+</sup>
298
299**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
300
301**系统能力:** SystemCapability.ArkUI.ArkUI.Full
302| 名称            | 类型    | 必填  | 说明                       |
303| -------------- | ------- | ------------------------- | ------------------------- |
304| weight        | number  | 是 | 字体原本的weight值。      |
305| to            | number  | 是 | 字体在应用中显示的weight值。 |
306
307## UIFontFallbackInfo<sup>11+</sup>
308
309**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
310
311**系统能力:** SystemCapability.ArkUI.ArkUI.Full
312| 名称            | 类型    | 必填  | 说明                       |
313| -------------- | ------- | ------------------------- | ------------------------- |
314| language       | string  | 是 | 字体集所支持的语言类型,语言格式为bcp47。    |
315| family         | string  | 是 | 字体集名,字体文件中指定的"family"值。 |
316
317**示例:**
318
319```ts
320// xxx.ets
321import { font } from '@kit.ArkUI';
322
323@Entry
324@Component
325struct FontExample {
326  build() {
327    Column() {
328      Button("getUIFontConfig")
329        .width('60%')
330        .height('6%')
331        .margin(50)
332        .onClick(()=>{
333          let fontConfig = font.getUIFontConfig();
334          console.log("font-dir -----------" + String(fontConfig.fontDir.length));
335          for (let i = 0; i < fontConfig.fontDir.length; i ++) {
336            console.log(fontConfig.fontDir[i]);
337          }
338          console.log("generic-------------" + String(fontConfig.generic.length));
339          for (let i = 0; i < fontConfig.generic.length; i ++){
340            console.log("family:" + fontConfig.generic[i].family);
341            for (let j = 0; j < fontConfig.generic[i].alias.length; j ++){
342              console.log(fontConfig.generic[i].alias[j].name + " " + fontConfig.generic[i].alias[j].weight);
343            }
344            for (let j = 0; j < fontConfig.generic[i].adjust.length; j ++){
345              console.log(fontConfig.generic[i].adjust[j].weight + " " + fontConfig.generic[i].adjust[j].to);
346            }
347          }
348          console.log("fallback------------" + String(fontConfig.fallbackGroups.length));
349          for (let i = 0; i < fontConfig.fallbackGroups.length; i ++){
350            console.log("fontSetName:" + fontConfig.fallbackGroups[i].fontSetName);
351            for (let j = 0; j < fontConfig.fallbackGroups[i].fallback.length; j ++){
352              console.log("language:" + fontConfig.fallbackGroups[i].fallback[j].language + " family:" + fontConfig.fallbackGroups[i].fallback[j].family);
353            }
354          }
355        })
356    }.width('100%')
357  }
358}
359```
360