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

hykeda7年前Vue1076

一、下载好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配置代理proxy实现跨域问题

最近在开发vite+vue项目时,调用接口获取服务器数据时提示接口有跨域问题,解决办法是配置代理:export default defineConfig({  //配置代理  se...

vite.config.js配置@

import { defineConfig } from 'vite'import vue from '@vitejs/plugin-vue'import path f...

vite+vue 插件/组件集合

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

vuex存储和本地存储(localstorage、sessionstorage)的区别

1. sessionStorage    sessionStorage 方法针对一个 session 进行数据存储。当用户关闭浏览器窗口后,数据会被删除。   用法: ...

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

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

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

vue3项目,Element-plus ElMessage API 调用样式丢失问题

1、Element-plus使用了自动按需导入,vite.config.js配置如下:defineConfig({     plugins: [...

发表评论    

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