当前位置:网站首页>Three ways to introduce external fonts in Mini Programs and problems
Three ways to introduce external fonts in Mini Programs and problems
2022-08-06 22:31:00 【i have a blue friend】
需求:After entering a title,Different fonts can be selected to display on the page.
Free commercial font download link:
链接: https://pan.baidu.com/s/1XLabx_xzC5kJycv7ejJU8Q 提取码: ka4r
方法一:本地引入
1、打开https://transfonter.org/
2、引入ttf或其他格式的文件,勾选banse64格式输出
3、Download the output file locally,引入到wxss里面
优点:加载速度快,The process of changing fonts is almost invisible,用户体验好
缺点:转为base64After the file is generally too large,If there are too many font files it will affect performance,The maximum value cannot be exceeded when the applet is packaged2M,Subcontracting is required;You can also specify text for output,This way the file will be much smaller.
方法二:cssIntroduced in the form of external links
1、将ttf文件放到服务器中;
2、新建一个wxss文件,写入以下代码:
@font-face {
font-family: "HarmonyOSHeiTi"; /* The font name here is custom */
src: url("https://h5.amemori.cn/h5/font-css/1.ttf"); /* 服务器上ttf文件的路径,Remember to configure domain authority */
font-weight: normal;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "ALiPuHui";
src: url("https://h5.amemori.cn/h5/font-css/2.otf");
font-weight: normal;
font-style: normal;
font-display: swap;
}
3、在需要用到的cssThe page imports this file and uses the font:
@import '../font/font-family.wxss';
text{
font-family: "HarmonyOSHeiTi";
}
优点:Does not occupy the applet memory
缺点:There is a delay when changing fonts,Users will clearly see the process of changing fonts.
方法三:jsIntroduced in the form of external links
1、在需要用到的页面的js文件,onLoad中写入:
wx.loadFontFaceIt is the WeChat official method of dynamically loading web fonts:https://developers.weixin.qq.com/miniprogram/dev/api/ui/font/wx.loadFontFace.html
onLoad(options) {
wx.loadFontFace({
family: 'HarmonyOSHeiTi',
source: 'url("https://h5.amemori.cn/h5/font-css/1.ttf")',
complete: function (res) {
console.log('插入字体'); console.log(res); },
success: function (res) {
console.log('成功'); console.log(res); },
fail: function (res) {
console.log('失败'); console.log(res); },
})
},
2、在wxss中直接使用familyThe font defined in .
The advantages and disadvantages are the same as method two
四、出现的问题以及解决方法:
开发工具和ios都显示正常,但是Android font settings are invalid
解决办法:服务器配置cors,即可解决.

边栏推荐
猜你喜欢

那些舍不得删除的 MP3--批量修改mp3的ID3tag

window 中 anaconda 下载与安装

信通院魏亮:产业数字化让传统赛道产生新赛道

This article takes you to understand the basic principles of webrtc (hands-on 1v1 video call)

12 MySQL optimization tips, the speed is more than ten times faster!

伪装马上线Cobalstrike

ASW3642 pin√pin替代TS3DV642方案,可使用原小板只需简单调整外围|ASW3642 HDMI二切一双向切换器方案

转移原理&指令运用

EXT.js学习【笔记】

微信小程序发布动态页面模板
随机推荐
ASW3642 pin√pin替代TS3DV642方案,可使用原小板只需简单调整外围|ASW3642 HDMI二切一双向切换器方案
Cesium快速上手1-CesiumWidget-Scene结构
一文带你了解webrtc基本原理(动手实现1v1视频通话)
也谈谈SDP零信任和传统公司接入的区别
3000字超详细介绍文件的最基本操作方法
B. Jumps
牛客/洛谷/沐枫——P1095 [NOIP2007 普及组] 守望者的逃离
random number from min to max
HCIP笔记(六)
猿人学-第三题
C language structure linked list node insertion method (before and after)
fasterrcnn tensorrt部署实践
Day021 抽象类和接口
IE彻底退出历史舞台 盘点那些年微软砍掉的产品
0x0000003b蓝屏什么原因 win7蓝屏0x0000003b怎么恢复
如何使用 saplink 安装其他网站上提供的 ABAP 程序
B. M-arrays
如何使用 saplink 安装其他网站上提供的 ABAP 程序试读版
如何在IDEA中创建接口和接口实现类
猿人学-第一题