1. 程式人生 > >微信小程式如何引用iconfont圖示

微信小程式如何引用iconfont圖示

最近在研究微信小程式,自己寫demo的時候想要引用巴里巴巴圖示庫的圖示,於是:

@font-face {
  font-family: 'iconfont';
  src: url('iconfont.eot');
  src: url('iconfont.eot?#iefix') format('embedded-opentype'),
  url('iconfont.woff') format('woff'),
  url('iconfont.ttf') format('truetype'),
  url('iconfont.svg#iconfont') format('svg');
}
.iconfont{ font-family:"iconfont"; font-size:16px;font-style:normal; -webkit-font-smoothing: antialiased; -webkit-text-stroke-width: 0.2px; -moz-osx-font-smoothing: grayscale; }

這是往常的寫法,但是發現這樣的寫法要報錯,後來查了一下,發現小程式的wxss檔案的font-face的url不接受http地址作為引數,但可以接受base64,因此需將字型檔案下載後,轉換為base64,然後引用。

方法:
在阿里巴巴圖示庫下載下來以後,將iconfont.ttf轉換即可。轉換地址:

https://transfonter.org/
這裡貼一個簡單的步驟:
這裡寫圖片描述
轉換後的目錄:這裡寫圖片描述
將css檔案在外部引入所要用的.wxss檔案中即可(@import ‘../../lib/style/lib.wxss’;)

最後,就可以使用啦:

.icon:after{
  font-family: 'iconfont';
  font-weight: 500;
  font-style: normal;
  content: '\e698;'
}