# wx.loadFontFace(Object object)

> 基础库 2.1.0 开始支持，低版本需做[兼容处理](1003706)。

> __以 [Promise 风格](/miniprogram/dev/framework/app-service/api.md#异步-API-返回-Promise) 调用__：支持
> 
> __小程序插件__：支持，需要小程序基础库版本不低于 [2.15.0](1003706)
> 
> __微信 Windows 版__：支持
> 
> __微信 Mac 版__：支持
> 
> __微信 鸿蒙 OS 版__：支持

## 功能描述

动态加载网络字体。

## 参数

### Object object


```json
[
  {
    "属性": "global",
    "类型": "boolean",
    "默认值": "false",
    "必填": "否",
    "说明": "是否全局生效",
    "最低版本": "[2.10.0](1003706)"
  },
  {
    "属性": "family",
    "类型": "string",
    "默认值": "",
    "必填": "是",
    "说明": "定义的字体名称",
    "最低版本": ""
  },
  {
    "属性": "source",
    "类型": "string",
    "默认值": "",
    "必填": "是",
    "说明": "字体资源的地址，可以为 https 链接或者 Data URL。",
    "最低版本": ""
  },
  {
    "属性": "desc",
    "类型": "Object",
    "默认值": "",
    "必填": "否",
    "说明": "可选的字体描述符",
    "最低版本": "",
    "子项": [
      {
        "结构属性": "style",
        "类型": "string",
        "默认值": "'normal'",
        "必填": "否",
        "说明": "字体样式，可选值为 normal / italic / oblique"
      },
      {
        "结构属性": "weight",
        "类型": "string",
        "默认值": "'normal'",
        "必填": "否",
        "说明": "字体粗细，可选值为 normal / bold / 100 / 200../ 900"
      },
      {
        "结构属性": "variant",
        "类型": "string",
        "默认值": "'normal'",
        "必填": "否",
        "说明": "设置小型大写字母的字体显示文本，可选值为 normal / small-caps / inherit"
      }
    ]
  },
  {
    "属性": "scopes",
    "类型": "Array",
    "默认值": "",
    "必填": "否",
    "说明": "字体作用范围，可选值为 webview / native / skyline，默认全选，设置 native 可在 Canvas 2D 下使用",
    "最低版本": ""
  },
  {
    "属性": "success",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用成功的回调函数",
    "最低版本": ""
  },
  {
    "属性": "fail",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用失败的回调函数",
    "最低版本": ""
  },
  {
    "属性": "complete",
    "类型": "function",
    "默认值": "",
    "必填": "否",
    "说明": "接口调用结束的回调函数（调用成功、失败都会执行）",
    "最低版本": ""
  }
]
```

#### object.success 回调函数
##### 参数

###### Object res

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| status  | string | 加载字体结果 |

#### object.fail 回调函数
##### 参数

###### Object res

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| status  | string | 加载字体结果 |

#### object.complete 回调函数
##### 参数

###### Object res

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| status  | string | 加载字体结果 |

## 示例代码

[在开发者工具中预览效果](https://developers.weixin.qq.com/s/b6Zrajm67R2x)
```js
wx.loadFontFace({
  family: 'Bitstream Vera Serif Bold',
  source: 'url("https://res.wx.qq.com/t/wx_fed/base/weixin_portal/res/static/font/33uDySX.ttf")',
  success: console.log
})
```

## Tips

1. 字体链接需要是下载类型。
2. 字体文件返回的 content-type 参考 [font](https://www.iana.org/assignments/media-types/media-types.xhtml#font)，格式不正确时会解析失败。
3. 字体链接必须是 https（ios不支持http)。
4. 建议格式为 TTF 和 WOFF，WOFF2 在低版本的iOS上会不兼容。
5. 字体链接必须是同源下的，或开启了cors支持，小程序的域名是`servicewechat.com`
6. 工具里提示 Faild to load font可以忽略
7. [2.10.0](1003706) 以前仅在调用页面生效。[2.10.0](1003706) 起支持全局生效，需在 `app.js` 中调用。
8. [3.7.9](1003706) 以前 scopes 默认值为 webview。[3.7.9](1003706) 起默认全选。
9. [3.7.9](1003706) 开始 source 支持传入 Data URL。
