【愚公系列】2022年08月 微信小程序-icon图标详解
前言
在小程序中经常会用到各种各样的图标,如果这些图标都使用图片的话,将会大大增加小程序打包后的体积,而小程序限制代码最大2MB,分包加载4MB,所以为了缩小体积,我们可以使用图标来缩小体积。
使用图标有两种方式:
- 自带图标
- 外部图标(图片、精灵图、CSS绘图、矢量字体、SVG格式)
自带图标组件属性说明:
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
type | string | 是 | icon的类型,有效值:success, success_no_circle, info, warn, waiting, cancel, download, search, clear | 1.0.0 | |
size | number/string | 23 | 否 | icon的大小,单位默认为px,2.4.0起支持传入单位(rpx/px),2.21.3起支持传入其余单位(rem 等)。 | 1.0.0 |
color | string | 否 | icon的颜色,同css的color | 1.0.0 |
一、自带图标
Page({
data: {
iconSize: [20, 30, 40, 50, 60, 70],
iconColor: [
'red', 'orange', 'yellow', 'green', 'rgb(0,255,255)', 'blue', 'purple'
],
iconType: [
'success', 'success_no_circle', 'info', 'warn', 'waiting', 'cancel', 'download', 'search', 'clear'
]
}
})
<view class="group">
<block wx:for="{{iconSize}}">
<icon type="success" size="{{item}}"/>
</block>
</view>
<view class="group">
<block wx:for="{{iconType}}">
<icon type="{{item}}" size="40"/>
</block>
</view>
<view class="group">
<block wx:for="{{iconColor}}">
<icon type="success" size="40" color="{{item}}"/>
</block>
</view>
二、实现图标的五种方案
1.图片
直接使用img标签实现图标会有以下三个缺点:
- 造成大量http请求
- 不方便修改颜色
- 放大图片会虚
2.精灵图
精灵图是把一组图片以非重合、最小化的方式排列成一张图片,在加载的时候只加载一次,这就减少了http请求。
在使用的时候,能过背景图片的定位与裁剪呈现某一块区域的图标。
<view class="gap">片7</view>
<icon class="sprite_icon scale2"></icon>
.sprite_icon {
display: block;
width: 80px;
height: 80px;
/* 此处在wxss中,可以使用网络图片,不能使用本地图片 */
background: url("https://cdn.nlark.com/yuque/0/2020/png/1252071/1589205723989-7de580b9-c9fd-4485-8b5b-3768f2f31bd0.png") -180px -310px;
}
缺点:精灵图解决了http请求过多的问题,但是它的颜色不能改变,放大会图片会变虚。
3.CSS绘图
<icon class="icon-close"></icon>
<icon class="icon-close scale2"></icon>
.icon-close {
display: inline-block;
width: 17px;
height: 2px;
background: green;
transform: rotate(45deg);
}
.icon-close::after {
content: '';
display: block;
width: 17px;
height: 2px;
background: red;
transform: rotate(-90deg);
}
.scale2{
transform-origin: 0 0 0;
transform:scale(2,2)
}
4.矢量字体
阿里图标库就是矢量字体的使用方案
阿里图标库官网:https://www.iconfont.cn/
转换iconfont.ttf文件得网址:https://transfonter.org/
4.1 使用步骤
下载到本地以后,解压出来有一个文件夹,里面有一个文件名字iconfont.css,和iconfont.ttf文件
1、iconfont.ttf文件进行转换
因为小程序的wxss文件的font-face的url不接受http地址作为参数,但可以接受base64,因此需将字体文件下载后,转换为base64,然后引用。
进入https://transfonter.org/平台
点击Add fonts按钮,添加iconfont.ttf文件,如上图中我选中的
上传完ttf文件后,勾选上两个勾选项
2.iconfont.ttf文件转换得内容并入iconfont.css文件中
@font-face {
font-family: 'iconfont';
src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAARUAA0AAAAACfwAAAP/AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP0ZGVE0cGh4GYACCQhEICodchkULDAABNgIkAxIEIAWFAgc8G3IIyB6FccNdYmjCElcYlvL/LYKntdd5u3sXBIWKXXQUO6TWsTEXWUW6yvYbV4Voq3M/5dm/QpNMirWYYiFc0w/zBC6A2z9Biv+rNdUBAGE2oONN7gII7l0IHVCRhKpQjfs6VgSOjO5UuGpbqbqY15hOwNcnPAgBb8/YCAEfo7VE0nsH+jEJJCErQkFCKRAvoCI9Y8/hOfbz8c9qCEFSZZAevv8gSglDz3g/6zzwvyG/1wAB6zOHhUx4ptK/eA1HYArXHFAUwU9+C37yWr07AUQNjS2MBCTIP0/A/pNoIYEK9GISxFOAeAVAAUBteM4MYw2RJiklSSwhSVJTUyqVSLrV68NGeoR6fd2IPbC25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normal;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "iconfont"; /* Project id */
src: url('iconfont.ttf?t=1646290057738') format('truetype');
}
.iconfont {
font-family: "iconfont" !important;
font-size: 16px;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.icon-paizhao:before {
content: "\e605";
}
.icon-erweima:before {
content: "\e606";
}
打开微信开发者工具,找打app.wxss,将刚才复制的内容粘贴至下面
在wxml中引用icon图标,使用如下
<view>
我是阿里扩展图标:
<text class='iconfont icon-paizhao'></text>
<text class='iconfont icon-erweima'></text>
</view>
5.SVG格式
使用svg这种方案,避免了格式转换,svg信息可以作为文本直接内嵌于程序中,不存在浏览器加载字体文件格式兼容的问题。简单方便,依赖少,因此它是Web开发中图标方案的首选。
<icon class="svg-icon"></icon>
.svg-icon{
display: block;
width: 200px;
height: 200px;
background-repeat: no-repeat;
background: url("data:image/svg+xml;base64,PHN2ZyB0PSIxNTg5MjEzNjE0NDc2IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjggMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjEzMDEiIHdpZHRoPSIyMDAiIGhlaWdodD0iMjAwIj48cGF0aCBkPSJNNTE0LjEzMzMzMyA1MDkuODY2NjY3bS0yNTYgMGEyNTYgMjU2IDAgMSAwIDUxMiAwIDI1NiAyNTYgMCAxIDAtNTEyIDBaIiBmaWxsPSIjZDQyMzdhIiBwLWlkPSIxMzAyIj48L3BhdGg+PHBhdGggZD0iTTUxNC4xMzMzMzMgMTcwLjY2NjY2N2MtMTkuMiAwLTMyLTE0LjkzMzMzMy0zMi0zMlYyOS44NjY2NjdjMC0xNy4wNjY2NjcgMTIuOC0yOS44NjY2NjcgMjkuODY2NjY3LTI5Ljg2NjY2N2gyLjEzMzMzM2MxNy4wNjY2NjcgMCAzMiAxNC45MzMzMzMgMzIgMzJ2MTA4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