

简体中文
动态加载网络字体,文件地址需为下载类型。微信小程序 '2.10.0'
起支持全局生效,需在 app.vue
中调用。
注意:
平台差异说明
5+App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节跳动小程序、飞书小程序 | QQ小程序 |
---|---|---|---|---|---|---|
1.9.0+ | 2.3.4+ | 基础库 2.1.0+ | 1.11.0+ | x | x | x |
参数说明
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
global | Boolean | false | 否 | 是否全局生效 |
family | String | 是 | 定义的字体名称 | |
source | String | 是 | 字体资源的地址。建议格式为 TTF 和 WOFF,WOFF2 在低版本的iOS上会不兼容。 | |
desc | Object | 否 | 可选的字体描述符 | |
success | Function | 否 | 接口调用成功的回调函数 | |
fail | Function | 否 | 接口调用失败的回调函数 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Object.desc 的结构
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
style | String | normal | 否 | 字体样式,可选值为 normal / italic / oblique |
weight | String | normal | 否 | 字体粗细,可选值为 normal / bold / 100 / 200../ 900 |
variant | String | normal | 否 | 设置小型大写字母的字体显示文本,可选值为 normal / small-caps / inherit |
代码示例
uni.loadFontFace({
family: 'Bitstream Vera Serif Bold',
source: 'url("https://sungd.github.io/Pacifico.ttf")',
success() {
console.log('success')
}
})
Tips
插件市场有加载字体的例子:https://ext.dcloud.net.cn/plugin?id=954
将rpx单位值转换成px
<script>
export default {
data() {
return {
boxWidth:""
}
},
onLoad() {
this.boxWidth = uni.upx2px(600) + 'px';
}
}
</script>