uni-app中使用iconfont字体,亲测有用

hykeda7年前Vue1272

一、下载好iconfont文件,复制iconfont.ttf这个文件至项目。

在你的项目中创建一个iconfont.css文件,文件内容:

@font-face {
  font-family: 'iconfont';  /* project id 441284 */
	src: url('//您的地址/iconfont.ttf') format('truetype');
}
.iconfont {
  font-family:"iconfont" !important;
  font-size:16px;
  font-style:normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.icon-shoucang:before{
	content: '\e61d';
}

这里面必须注意 &#xe600 ;要转成:\e600,如果你要使用class的话

然后在App.vue文件中引入该css:

@import './common/iconfont1.css';

在具体页面使用:

1、<view class="iconfont"> &#xe600 ; </view>
2、<view class="iconfont icon-shoucang"></view>

第一种为推荐方法,第二种最后的文件必须在40K之内,不然无法使用,这个要注意。

二、iconfont文件里面包含 iconfont.ttf、iconfont.css, 将 iconfont.ttf文件转为 base64。

推荐转换工具地址:https://www.giftofspeed.com/base64-encoder/

然后打开 iconfont.css文件,修改 @font-face 部分,将转换好的 base64代码粘贴到对应位置,并且设置 代码如下:

@font-face {              
font-family: "iconfont";
src: url(data:font/truetype;charset=utf-8;base64,转换的base64内容) format('truetype');
}
.iconfont {
display: inline-block;
}

最后则再项目中App.vue中引入iconfont.css文件

<style>   
        @import "./font/iconfont.css";   
 </style>

在任意组件中使用方法:

<view class="iconfont icon-XXX"></view>  

经测试在H5及小程序中可以正常使用

标签: uni-appiconfont

相关文章

使用vite打包vue项目生成dist文件夹,部署至nginx除了index.html,其他路径都提示404错误的解决

使用vite打包vue项目生成dist文件夹,部署至nginx除了index.html,其他路径都提示404错误的解决

用vue打包生成dist文件夹,然后在本地nginx部署vue项目时只能访问默认页面,刷新和跳转页面都会出问题。原因:只访问了dist文件下的 index.html、index.htm页面,...

在写css时不起作用,请排查是否是scoped的问题

在写css的时候,用了框架自带的组件table和card,把table包在了card里面,然后发现table的分页样式出现了问题,查看源码后发现card中写了ul li { margin-left:2...

页面直接使用ElementUI,官方提供的表格代码,样式错乱

页面直接使用ElementUI,官方提供的表格代码,样式错乱

在使用element编写前端代码是,引入element的css和js,使用官方提供的例子:<template>   <el-table :data=&...

vite+vue 插件/组件集合

自动引入插件:https://github.com/antfu/unplugin-auto-importvue的按需组件自动导入:https://github.com/antfu/unplugin-v...

pinia中数据解构

在vuex中获取state中的数据可以解构的方式如下:import { mapState,mapMutations } from "vuex...

Vue3二维码生成

一、装上插件npm install --save qrcode.vue二,引入插件到页面中import QrcodeVue from ...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。