
简体中文
动态加载网络字体
Web | 微信小程序 | Android | iOS | HarmonyOS |
---|---|---|---|---|
4.0 | 4.41 | √ | 4.10 | 4.61 |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | LoadFontFaceOptions | 是 | - | - | - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | - | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
errCode | number | 是 | - | - | 错误码 | |||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
status | string | 否 | - | 加载字体结果 | ||||||||||||||||||||||||||||
errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | |||||||||||||||||||||||||||
data | any | 否 | - | - | 错误信息中包含的数据 | |||||||||||||||||||||||||||
cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | |||||||||||||||||||||||||||
errMsg | string | 是 | - | - | - |
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | - | - |
类型 | 必备 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Promise<AsyncApiSuccessResult> | 否 | ||||||||||||
|
app-ios 平台
加载字体一定是全局生效,不支持通过global属性设置为非全局生效url()
包裹。可能某些平台不包裹也可以生效,但标准规范是包裹,按标准写法才能全端生效。如下:uni.loadFontFace({
global: true,
family: 'UniFontFamily',
source: "url('/static/font/uni.ttf')", //需使用url方法包裹。本地字体请放在/static目录下,否则打包时不会把字体文件打进去。也支持网络字体
success() {
console.log('global loadFontFace uni.ttf success')
},
fail(error) {
console.warn('global loadFontFace uni.ttf fail', error.errMsg)
},
})
HarmonyOS 注意事项
不同平台支持的字体格式不同,另见css字体
Template
Script
<template>
<page-head title="loadFontFace"></page-head>
<view class="uni-padding-wrap">
<text class="font-size-20">全局加载字体:</text>
<text class="font-size-20 line-height-40" style="font-family: UniFontFamily">font-family: uni.ttf</text>
<view style="flex-direction: row;">
<!-- 微信小程序只支持加载网络字体 -->
<!-- #ifdef MP-WEIXIN -->
<text class="font-size-20" style="font-family: UniFontFamily2;">{{uniIcon3}}</text>
<text class="icon-text">\ue102</text>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<text class="font-size-20" style="font-family: UniFontFamily;">{{uniIcon1}}</text>
<text class="icon-text">\ue100</text>
<text class="font-size-20" style="font-family: UniFontFamily;">{{uniIcon2}}</text>
<text style="margin-left:5px;line-height:22px;">\ue101</text>
<!-- #endif -->
</view>
<!-- #ifndef APP-HARMONY -->
<text class="uni-common-mt font-size-20">非全局加载字体:</text>
<!-- #endif -->
<text class="font-size-20 line-height-40">font-family: uni.ttf(base64格式)</text>
<view style="flex-direction: row;">
<text class="font-size-20" style="font-family: UniFontFamily2;">{{uniIcon3}}</text>
<text class="icon-text">\ue102</text>
</view>
<text class="font-size-20 line-height-40" style="font-family: AlimamaDaoLiTiTTF">font-family: 阿里妈妈刀隶体-ttf
(网络字体下载后生效)</text>
<text class="font-size-20 line-height-40" style="font-family: AlimamaDaoLiTiOTF">font-family:
阿里妈妈刀隶体-otf</text>
<text class="item" style="font-family: AlimamaDaoLiTiWOFF">font-family: 阿里妈妈刀隶体-woff</text>
<text class="item" style="font-family: AlimamaDaoLiTiWOFF2">font-family: 阿里妈妈刀隶体-woff2</text>
<button class="uni-btn" @click="navigateToChild">跳转子页面测试字体生效范围</button>
</view>
</template>
<style>
.font-size-20 {
font-size: 20px;
}
.line-height-40 {
line-height: 40px;
}
.icon-text{
margin-left:5px;
margin-right: 20px;
line-height:22px;
}
</style>
名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
errMsg | string | 是 | - | 错误信息 |