Iconfont该如何使用,多种组件模块化详解(png,icon,svg)
前言
Iconfont是一个功能强大、资源丰富的在线图标库,通过掌握其使用方法和多种组件的模块化应用技巧,我们可以更好地利用图标进行设计和管理,提升作品的质量和用户体验。接下来,本文将详细介绍Iconfont的使用方法以及各种图标格式的模块化应用技巧
- iconfont官网:iconfont-阿里巴巴矢量图标库
一、下载png格式
- 前端网页开发科通过img标签引入图片
<img src="./avatar.png" alt="" />
- 微信小程序等用image标签引入图片
<image src="./avatar.png" />
二、复制Svg代码 ,直接使用
<svg t="1713362279012" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="993" width="200" height="200"><path d="M766.492701 468.808534c-44.584739 68.326113-124.725808 106.780451-218.35376 119.598563 4.458474 25.636225 0 63.979101 22.292369 102.433439 44.584739 81.144226 80.252531 170.871013 93.516491 234.961576 75.794057 29.871775 231.729183-42.689888 276.20246-136.652226 49.043213-102.5449 0-205.089801-80.141069-213.560902s-124.725808 34.107326-115.80886 81.144226 53.390225 46.925438 102.433438 17.053663 93.516491 34.218787 40.126266 59.855012-133.754218 46.925438-178.338958-4.347012c-49.043213-51.27245-44.584739-234.850114 151.476652-217.796451S1047.042174 780.232938 984.735 861.711549C922.316365 955.673887 846.63377 1024 624.490307 1024H267.812392c-89.169479 0-26.750844-179.342114 53.390226-328.812452 22.29237-42.80135 17.833896-76.908675 22.292369-102.544901-97.974965-13.041036-178.338957-51.27245-218.465222-119.598563H67.181065a27.419615 27.419615 0 0 1-22.292369-25.636225 20.620442 20.620442 0 0 1 22.292369-21.400675h35.667792a121.270491 121.270491 0 0 1-8.916948-42.689888H22.596326a21.400675 21.400675 0 1 1 0-42.689888h66.877109a224.372701 224.372701 0 0 1 44.584739-140.999238V28.868619c0-21.400675 17.833896-34.218787 40.126266-25.636225L374.481381 89.169479A427.902035 427.902035 0 0 1 446.15135 84.376619 354.894525 354.894525 0 0 1 513.028458 89.169479L713.659785 3.789703c22.29237-8.582562 40.126265 0 40.126266 25.636225v170.202242a243.989986 243.989986 0 0 1 44.584739 140.999238h66.877109c17.833896 0 26.750844 8.582562 26.750844 21.400675a20.620442 20.620442 0 0 1-22.29237 21.289213h-71.335583c-4.458474 17.165125-4.458474 29.871775-8.916948 42.689888H825.121634a21.400675 21.400675 0 1 1 0 42.80135z" fill="#658FFF" p-id="994"></path></svg>
三、如何引用svg图标组件
1、创建我的项目(datav-libs-dev)
搜索心仪的图标,添加至data-libs-dev项目中
2、icomoon.io在线图标编辑器
icomoon.io是一个在线的图标字体生成器与编辑器网站,它允许用户创建和编辑自己的图标字体库,以及将图标转换为可嵌入网页的字体格式。通过icomoon,用户可以选择和定制图标,然后生成一个字体文件,该文件可以直接在网页项目中使用。
该网站提供了大量的图标供用户选择,包括各种常见的形状、符号和图像。用户还可以上传自己的SVG图标进行编辑和转换。icomoon同时支持多种字体格式,如woff、woff2、ttf等,以适应不同的项目需求。
此外,icomoon还提供了图标搜索功能,方便用户快速找到所需的图标。用户还可以创建自己的图标集,以便在多个项目之间共享和使用。
- IcoMoon.io官网:IcoMoon.io官网链接
3、使用下载的svg图标
如何将下载的图标放入iconfont项目中,在iconfont中创建我的项目并打开,如果选择上传svg图标,点击右侧上传图标至项目,将svg下载的图标拖拽至框内上传即可
4、引用svg图标组件
- 点击symbol,生成在线链接
- 打开需要使用的项目,如我使用的是vue3框架,则需要将链接放入全局里,public文件夹下的index.html里
<script src="//at.alicdn.com/t/font_3377923_iv89pllgqsa.js"></script>
- 然后创建svg组件
<template>
<div class="icon-wrapper" :style="{ ...style }">
<svg class="icon" >
<use :href="iconName"></use>
</svg>
</div>
</template>
<script>
export default {
name: 'Icon',
props: {
name: String,
prefix: {
type: String,
default: 'icon-'
},
style: Object
},
setup(ctx) {
const iconName = `#${ctx.prefix}${ctx.name}`
return { iconName }
}
}
</script>
<style lang="scss" scoped>
.icon {
width: 100%;
height: 100%;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
.icon-wrapper{
display: inline-block;
}
</style>
- 父组件调用svg组件
<icon prefix="icon-" class="set" name="dongmanxiao" ></icon>
四、引用icon图标组件(上)
1、下载至本地
首先将需要使用的图标放入我的项目,然后选择下载至本地
2、压缩包放入并解压
桌面创建iconfont文件夹,将压缩包放入并解压,此时iconfont.css文件正式我们需要的
3、微信小程序引入iconfont
如果是在微信小程序中引入iconfont.css,此时后面会发生报错,原因是不支持直接只用@font-face,需要转成base64编码
4、base64编码
base64编译转码免费网站:transfonter.org官网链接
选择打开Base64 encode
5、点击Add fonts上传
6、上传的下载文件中的woff文件
7、点击conver转变
8、点击download下载
9、解压找到stylesheet.css文件
10、打开最初下载iconfont文件iconfont.css
将stylesheet.css里的@font-face替换掉iconfont.css里的@font-face
这样iconfont就转码base64编译成功啦
五、引用icon图标组件(下)
1、打开微信开发者工具
创建一个微信小程序项目,在项目文件夹下创建 iconfont/index.wxss
2、index.wxss输入下载的iconfont.css内容
@font-face {
font-family: 'iconfont';
src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAAX8AA0AAAAAC0AAAAWlAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP0ZGVE0cGh4GYACCYhEIColMhzwLEgABNgIkAyAEIAWFAgdZGzQJyB6FcbNzZGRZo86XPzWIaKzN3t2LSXq0BDHt0C27RhKJUrRk1Ub0H/hb9Rr3G7gR0cBpwAZCLdTeeQgtw4kLKCLZ3bYPxOJQUw2li0vTlM4B3XQ7sCj1QqV6adSqpVsVCoZqp3SrmAfMvNDrm77TD2yXiE3tBQjANZ9eNgDce872cILQW7qBKIBC0NgQDKAAY1yyDQvUCtcJAFvg98sHmIkAKCwaYf+O45kIG3dQd0a4qRwq+hJoAAEwk8el1rw6He4awY24f4KHCoAbOwrzjLqD3BG8o3pnhMMB8OdyXLIfZN/L/i8bCSgEr1hBmLYUoIj88wDZ5J/Ta4odtB/l5pRFIJ+ANyLYLCj10KAMWijsoiisIitMLaFLhddKkn9EBFelI+qwkfUYCvPaBRlyA1U2vHERI7S2LB/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('woff');
font-weight: normal;
font-style: normal;
font-display: swap;
}
.iconfont {
font-family: "iconfont" !important;
font-size: 16px;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.icon-zhizhen:before {
content: "\e60a";
}
.icon-logo1:before {
content: "\e621";
}
.icon-logo:before {
content: "\e60c";
}
.icon-dongmanxiao:before {
content: "\e6ac";
}
.icon-shezhi1:before {
content: "\e601";
}
3、在components组件文件夹下创建icon组件
- icon.js
// components/icon/icon.js
Component({
/**
* 组件的属性列表
*/
properties: {
name:String,
color:{
type:String,
value:"#6faf92"
},
size:{
type:String,
value:"34"
}
},
/**
* 组件的初始数据
*/
data: {
},
/**
* 组件的方法列表
*/
methods: {
}
})
- icon.wxml
<view class="iconfont icon-{{name}}" style="font-size: {{size}}rpx; color:{{color}};"></view>
- icon.wxss
@import "../../iconfont/index.wxss"
4、在app.json中创建全局使用
"usingComponents":{
"i-icon": "/components/icon/icon"
}
5、引用icon组件
可用过size控制icon大小,color控制颜色,name控制图标
<i-icon name="dongmanxiao" size="20" color="#fff" />
- 点赞
- 收藏
- 关注作者
评论(0)