本模块为应用提供第三方字体的安装、卸载、查询以及字体服务状态监听能力。具体为:
起始版本: 26.0.1
表示字体作用范围的枚举。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Global.FontManager
| 名称 | 值 | 说明 |
|---|---|---|
| APP | 0 | 应用级字体。字体的生命周期跟随应用的生命周期,应用退出或字体服务异常退出时,安装的字体文件会被自动清理/卸载。需先调用fontManager.onFontObserver注册监听后才能安装。 |
| SESSION | 1 | 会话级字体。字体的生命周期不跟随应用的生命周期,设备重启或当前用户退出(多用户场景下)时,安装的字体文件会被自动清理/卸载。 |
字体服务状态变化监听器。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Global.FontManager
onServiceDied(): void
字体服务异常退出时的回调函数,应用可在此回调函数中执行资源清理或重新注册等操作。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Global.FontManager
示例:
import { fontManager } from '@kit.LocalizationKit';
const observer: fontManager.FontClientObserver = {
onServiceDied: () => {
console.info('font service died');
}
}; installScopeFont(url: string, scope: FontScope): Promise<void>
安装指定路径下的字体文件为应用级或会话级字体。使用Promise异步回调。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.UPDATE_SCOPE_FONT
系统能力: SystemCapability.Global.FontManager
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| url | string | 是 | 待安装的字体文件路径,仅支持.ttf、.ttc和.otf格式的字体文件。 |
| scope | FontScope | 是 | 字体作用范围。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 31100101 | The font does not exist. |
| 31100102 | The font is not supported. |
| 31100103 | Failed to copy the font file. |
| 31100104 | The font file is installed. |
| 31100105 | Exceeded the maximum number of installed files. |
| 31100110 | Call failed due to system error. |
| 31100115 | The font observer is not registered. |
示例:
import { fontManager } from '@kit.LocalizationKit';
async function installScopeFont() {
try {
await fontManager.installScopeFont('fontPath', fontManager.FontScope.APP);
console.info('installScopeFont suc');
} catch (error) {
console.error('installScopeFont err.' + error.code);
}
} uninstallScopeFont(url: string): Promise<void>
根据字体路径卸载已安装的应用级或会话级字体。使用Promise异步回调。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.UPDATE_SCOPE_FONT
系统能力: SystemCapability.Global.FontManager
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| url | string | 是 | 需要卸载的字体路径。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 31100108 | Failed to delete the font file. |
| 31100110 | Call failed due to system error. |
| 31100112 | The scope font is not found. |
示例:
import { fontManager } from '@kit.LocalizationKit';
async function uninstallScopeFont() {
try {
await fontManager.uninstallScopeFont('fontPath');
console.info('uninstallScopeFont suc');
} catch (error) {
console.error('uninstallScopeFont err.' + error.code);
}
} getFontScope(url: string): Promise<FontScope>
查询指定路径字体的作用范围。使用Promise异步回调。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.UPDATE_SCOPE_FONT
系统能力: SystemCapability.Global.FontManager
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| url | string | 是 | 需要查询的字体路径。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<FontScope> | Promise对象,返回字体的作用范围。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 31100110 | Call failed due to system error. |
| 31100112 | The scope font is not found. |
示例:
import { fontManager } from '@kit.LocalizationKit';
async function getFontScope() {
try {
let scope = await fontManager.getFontScope('fontPath');
console.info('font scope is ' + scope);
} catch (error) {
console.error('getFontScope err.' + error.code);
}
} onFontObserver(observer: FontClientObserver): void
注册字体服务状态变化监听器。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.UPDATE_SCOPE_FONT
系统能力: SystemCapability.Global.FontManager
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| observer | FontClientObserver | 是 | 字体服务状态变化监听器。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 31100110 | Call failed due to system error. |
| 31100113 | The font observer is already registered. |
| 31100114 | The maximum number of font observers has been reached. |
示例:
import { fontManager } from '@kit.LocalizationKit';
const observer: fontManager.FontClientObserver = {
onServiceDied: () => {
console.info('font service died, please clean up resources');
}
};
try {
fontManager.onFontObserver(observer);
console.info('onFontObserver suc');
} catch (error) {
console.error('onFontObserver err.' + error.code);
} offFontObserver(): void
注销字体服务状态变化监听器。
起始版本: 26.0.1
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.UPDATE_SCOPE_FONT
系统能力: SystemCapability.Global.FontManager
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 31100110 | Call failed due to system error. |
| 31100115 | The font observer is not registered. |
示例:
import { fontManager } from '@kit.LocalizationKit';
try {
fontManager.offFontObserver();
console.info('offFontObserver suc');
} catch (error) {
console.error('offFontObserver err.' + error.code);
}