has.loadFontFace(Object object)
Loads a font from the specified network address. Make sure the address points to a downloadable resource.
For details about content-type returned by the font file, see font. Check whether the file format is correct. An incorrect format will lead to a parsing failure.
Fonts added via APIs are not supported by same-layer rendering components such as canvas.
The font URLs must start with https. Since the network font loading is affected by network conditions, you are advised to check the logs to confirm that the font file is downloaded successfully.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| global | boolean | false | No | Whether to take effect globally. |
| family | string | - | Yes | Font name. |
| source | string | - | Yes | Address of the font resource. The recommended formats are TTF and WOFF. |
| desc | Object | - | No | Optional font descriptor. |
| success | function | - | No | Callback triggered when the API call is successful. |
| fail | function | - | No | Callback triggered when the API call fails. |
| complete | function | - | No | Callback triggered when the API call is complete, regardless of success or failure. |
desc parameters
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| style | string | 'normal' | No | Font style, which can be set to normal, italic, or oblique. |
| weight | string | 'normal' | No | Font weight, which can be set to normal, bold, 100, 200, ..., or 900. |
| variant | string | 'normal' | No | Font variant for displaying text in small capitals, which can be normal, small-caps, or inherit. |
Return value of success
| Parameter | Type | Description |
|---|---|---|
| status | string | Execution result of font loading. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: 1.option should be object. 2.family should be not empty string. 3.source should be not empty string. 4.option.desc should be object. 5.desc.style is Invalid. 6.desc.weight is Invalid. 7.desc.variant is Invalid. |
| 30001301 | load font face error. |
Example:
const self = has.loadFontFace({
family: 'myFamily',
source: 'url("https://www.example.com/xxx.ttf")', // Replace the example address with the actual font resource address.
success: (res) => {
console.info('loadFontFace success', res);
},
fail: (err) => {
console.error('loadFontFace fail', err);
},
complete: (res) => {
console.info('loadFontFace complete', res);
}
});