字体更新时间:2025-12-24 18:13:03
基础信息说明
功能概述:加载自定义字体文件、获取一行文本的行高。
| 项目 | 说明 |
| 版本要求 | 无 |
| 前提条件 | 无 |
| 使用限制 | 无 |
| 相关教程 | 无 |
方法说明
| 方法名 | 签名 | 同步/异步 | 说明 |
| loadFont | qg.loadFont(String path) :String | 同步 | 加载自定义字体文件,成功返回字体family 值,失败返回 null |
| getTextLineHeight | qg.getTextLineHeight(Object object) : Number | 同步 | 计算指定文本的一行行高,单位 px |
参数说明
qg.loadFont(String path)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| path | String | - | 是 | 字体文件路径,可以是代码包文件路径,也可以是qg.env.USER_DATA_PATH + ${文件地址} |
qg.getTextLineHeight(Object option)
| 属性名 | 类型 | 默认值(如有) | 必填 | 说明 |
| fontStyle | String | - | 否 | 字体样式 ,合法值有 'normal' 正常、'italic' 斜体,默认值为 'normal' |
| fontWeight | String | - | 否 | 字重 , 合法值有 'normal' 正常、'bold' 粗体,默认值为 'normal' |
| fontSize | Number | - | 否 | 字号,默认值为 16 |
| fontFamily | String | - | 是 | 字体名称 |
| text | String | - | 是 | 文本的内容 |
| success | Function | - | 否 | 接口调用成功的回调函数 |
| fail | Function | - | 否 | 接口调用失败的回调函数 |
| complete | Function | - | 否 | 接口调用结束的回调函数 |
返回值说明
qg.loadFont
| 属性名 | 类型 | 说明 |
| fontFamily | String | 如果加载字体成功,则返回字体 family 值,否则返回 null |
qg.getTextLineHeight
| 属性名 | 类型 | 说明 |
| textLineHeight | Number | 行高 |
回调结果说明
qg.getTextLineHeight
| 属性名 | 类型 | 说明 |
| success | function | 行高计算成功的回调函数 |
| fail | function | 参数缺失或计算失败的回调函数 |
| complete | function | 接口调用结束的回调函数(调用成功、失败都会执行) |
示例代码
const fontFamily = qg.loadFont('/common/font/m8JVjfNVeKWVnh3QMuKkFcZVaUuH99GUDg.woff2');
if (fontFamily) {
console.log('fontFamily:', fontFamily);
} else {
console.log('no fontFamily!');
}
const textLineHeight = qg.getTextLineHeight({
fontFamily: fontFamily,
text: 'I am Arial',
success: function () {
console.log('success 获取行获取行高成功 字体:', fontFamily)
},
fail: function (err) {
console.error('fail 获取行高失败 err:', err)
},
complete: function (res) {
console.log('complete 获取行高完成 res', res)
}
})
console.log('字体:', fontFamily, '行高为:', textLineHeight)上一篇:帧率
下一篇:提示框
文档内容是否有帮助?
有帮助
无帮助