小程式中 icon 顏色自定義解決方案

Chris威發表於2018-07-11

背景

隨著公司業務的發展,現在的產品真的是越來越 體(sang)貼(xin)用(bing)戶(kuang) 了,剛好前陣子準備做一個後臺可以讓用使用者 DIY 自己的小程式,不小心看到了 某網 提供了一套圖示庫,使用者可以自主選擇,並且可以進行顏色配置的時候,我就感覺事情不妙~

當時腦子一熱,桌子一拍,我們也需要提供這樣的功能,而且要做的比他們還強大...

作為開發的我默默的留下了這張圖~

小程式中 icon 顏色自定義解決方案

我們知道小程式裡面是不支援類似 ttf woff 這類字型的,直接引入編譯都過不去~

這個就是要告訴我們,我們不能像 H5 那樣直接把字型下載下來然後引入到樣式中,這條道路是行不通了,只能重新找找方案了~

嗯,我也不 藏著掖著了 , 接下來給大家 安利 2 中解決方案~

解決方案

方案一

利用小程式中不支援 ttf 字型,卻支援 base64 的特點,我們可以通過將 ttf 字型轉碼成 base64 ,再通過 iconfont 的特點,通過設定字型大小和顏色來達到目的。

其具體實現如下:

  • 1.我們可以先去阿里提供的圖示庫 官網 下載我們需要的圖示,一般來說都夠用了,這裡我隨便好了 3 個作為測試用 , 點選 下載程式碼 將程式碼下載到本地~

小程式中 icon 顏色自定義解決方案

  • 2.解壓我們剛才下載的包,會有以下這些檔案,但對於我們來說,會用到的只有 iconfont.cssiconfont.ttf 這兩個檔案,

小程式中 icon 顏色自定義解決方案

  • 3.我們要對 iconfont.ttf 這個檔案進行轉碼,將它轉成 base64 格式,剛好 這個網站 就提供了這個功能(pass:這個網站可能會有點慢,可以的話代理加個速),按照下圖的步驟,分別上傳 ttf 檔案,選擇配置,點選轉換。

小程式中 icon 顏色自定義解決方案

  • 4.轉換完成如下圖所示,廢話不多說,直接下載下來解壓,我們需要的是裡面 stylesheet.css 這個檔案

小程式中 icon 顏色自定義解決方案

  • 5.我們在小程式裡的靜態資原始檔夾裡新建一個叫 iconfont.wxss 樣式檔案,將 上面解壓出來的 stylesheet.css 裡面的內容全部拷貝到 iconfont.wxss 中,另外,還記得我們上面說的 iconfont.css 嗎?這時候就要派上用場了,我們開啟 iconfont.css 這個樣式檔案,將下面的樣式,也就是我用紅框標註的內容複製到 iconfont.wxss 檔案裡,注意上面的不需要複製!!

小程式中 icon 顏色自定義解決方案

到此為止,準備工作算是已經完成了,這時候你的 iconfont.wxss 應該類似下面這樣

iconfont.wxss

@font-face {
    font-family: 'iconfont';
    src: url(data:font/truetype;charset=utf-8;base64,AAEAAAANAIAAAwBQRkZUTYR2fJ0AAAm4AAAAHEdERUYAKQANAAAJmAAAAB5PUy8yVuZIqQAAAVgAAABWY21hcLOyB+EAAAHMAAABWmdhc3D//wADAAAJkAAAAAhnbHlmRcqmoQAAAzgAAAOQaGVhZBHwmZAAAADcAAAANmhoZWEH3AOIAAABFAAAACRobXR4EYQAuAAAAbAAAAAabG9jYQHOArIAAAMoAAAAEG1heHABFgBdAAABOAAAACBuYW1lKeYRVQAABsgAAAKIcG9zdHMZwLsAAAlQAAAAQAABAAAAAQAAcD2OKF8PPPUACwQAAAAAANdpKsoAAAAA12kqygAB/4ED/gN+AAAACAACAAAAAAAAAAEAAAOA/4AAXAQBAAAAAAP+AAEAAAAAAAAAAAAAAAAAAAAGAAEAAAAHAFEABQAAAAAAAgAAAAoACgAAAP8AAAAAAAAAAQP6AZAABQAAAokCzAAAAI8CiQLMAAAB6wAyAQgAAAIABQMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUGZFZABAAHjmvQOA/4AAXAOAAIAAAAABAAAAAAAABAAAAAAAAAABVQAAA+kALAQAAIsEAQABAEUAAAAAAAMAAAADAAAAHAABAAAAAABUAAMAAQAAABwABAA4AAAACgAIAAIAAgB45qLms+a9//8AAAB45qLms+a9////ixliGVIZSQABAAAAAAAAAAAAAAAAAQYAAAEAAAAAAAAAAQIAAAACAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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ZQBjAHQALgAAR2VuZXJhdGVkIGJ5IHN2ZzJ0dGYgZnJvbSBGb250ZWxsbyBwcm9qZWN0LgAAaAB0AHQAcAA6AC8ALwBmAG8AbgB0AGUAbABsAG8ALgBjAG8AbQAAaHR0cDovL2ZvbnRlbGxvLmNvbQAAAgAAAAAAAAAKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHAAAAAQACAFsBAgEDAQQDbWFwB3N1Y2Nlc3MDYXRtAAAAAf//AAIAAQAAAAwAAAAWAAAAAgABAAMABgABAAQAAAACAAAAAAAAAAEAAAAA1aQnCAAAAADXaSrKAAAAANdpKso=) format('truetype');
    font-weight: normal;
    font-style: normal;
}

.iconfont {
  font-family:"iconfont" !important;
  font-size:16px;
  font-style:normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-map:before { content: "\e6a2"; }

.icon-success:before { content: "\e6b3"; }

.icon-atm:before { content: "\e6bd"; }

複製程式碼
  • 6.接著,將我們的字型檔案引入到 app.wxss 全域性 , 這樣你就可以在任何你想用到的地方進行呼叫了,只需要將 class 寫對就沒什麼問題了,

    <!--index.wxml-->
    <view class="container">
        <view class='block'>
            <text class='iconfont icon-map'></text>
            <text>地圖</text>
        </view>
        <view class='block'>
            <text class='iconfont icon-success'></text>
            <text>成功</text>
        </view>
        <view class='block'>
            <text class='iconfont icon-atm'></text>
            <text>atm</text>
        </view>
    </view>
    複製程式碼

    怎麼樣,成功了吧!

    小程式中 icon 顏色自定義解決方案

    什麼? 你要換顏色,難不倒我的,只要改變對應的 color 就可以了~

    <!--index.wxml-->
    <view class="container">
        <view class='block'>
            <text class='iconfont icon-map' style='color : red'></text>
            <text>地圖</text>
        </view>
        <view class='block'>
            <text class='iconfont icon-success' style='color : green'></text>
            <text>成功</text>
        </view>
        <view class='block'>
            <text class='iconfont icon-atm' style='color : blue'></text>
            <text>atm</text>
        </view>
    </view>
    複製程式碼

小程式中 icon 顏色自定義解決方案

嗯,是不是可以插會兒腰了~

小程式中 icon 顏色自定義解決方案

且慢,剛不是說兩種方案麼,怎麼到這裡就叉起腰來了,還沒完呢,等會嘚瑟哈!!

方案二

剛介紹了方案一,確實是一種選擇,我們只需約定好樣式,類名,後端將顏色什麼的返回給我們就好了~

但是這種方案存在弊端,不夠靈活,如果我們要更新我們的圖示庫,那麼我們上面的流程又得走一遍,如果後面圖示庫的型別變多,那麼我們轉碼後的檔案會隨之變大,這對於小程式來說是致命的,因為做小程式都知道,我們都在時刻想著如何用最少的程式碼發揮最大的價值!

所以,這種方案有一定的侷限性,因此才出了第二套方案~

第二套方案的思路是藉助 svg 向量圖具有不失真和可以自主填充顏色的特點,通過 fill 屬性,使用者可以自定義填充顏色,但是小程式裡面不支援 svg 標籤的,我們通過顏色選擇器將使用者選擇好的顏色填充到 svg 裡,在通過轉碼將換過顏色的 svg 轉成 base64 編碼發給小程式,小程式可以直接用在 image 標籤中的 src 屬性上~

我們一起來試試~

  • 1.我們還是先去阿里提供的圖示庫 官網 下載我們需要的圖示,這次跟第一次有些不一樣,這次我們要選擇下載素材,選擇 svg~

小程式中 icon 顏色自定義解決方案

  • 2.將下載好的 svg 原始碼直接放在我們的圖示庫裡面,選擇某個圖示之後,通過顏色選擇器我們可以通過 js 設定對應的 svg 圖示的 fill 屬性,動態的給圖示填充不同的顏色,差不多就跟下面的效果有點相似~

小程式中 icon 顏色自定義解決方案

  • 3.接下來可以將換過顏色的 svg 通過 svgbase64 的外掛將其轉換成 base64 編碼,這裡為了方便,我直接偷懶,線上轉換了,對應的 base64 編碼如下:
<!--map-->
data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyBjbGFzcz0iaWNvbiIgd2lkdGg9Ij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nzg1IDExLjQ1OTMyNS0yNS41OTcxMzMgMjUuNTk3MTMzLTI1LjU5NzEzM3MyNS41OTcxMzMgMTEuNDYwMzQ4IDI1LjU5NzEzMyAyNS41OTcxMzNsMCA3Ni43MzcxMzNDNzQ5LjI2Njk5NCA1MzQuODQwODM0IDczNy44MDY2NDYgNTQ2LjMwMDE1OCA3MjMuNjY5ODYxIDU0Ni4zMDAxNTh6IiAvPjwvc3ZnPg==
複製程式碼
  • 4.接著我們只需要把對應的 base64 編碼發給小程式端,小程式直接放在 image 元件中的 src 屬性即可,對於樣式來說,可以直接修改 image 的樣式來控制 icon 樣式,效果如下:

小程式中 icon 顏色自定義解決方案

ok~ 還算是比較圓滿的完成了任務~

這下產品總不會刁難我了吧~

結語

到此,我們已經 get 到了兩種小程式中實現 icon 自定義顏色的解決方案,對比發現,第一種方案的特點是實現起來比較簡單,步驟什麼的也都還ok,就是擴充性不強,每次改動都需要重新再來一遍,而且對於比較多的圖示來說小程式空間佔用較大,有一定的侷限性,而第二種方案相比於第一種來說雖然在實現過程中稍微繁瑣了一些,但是帶來的效果是顯而易見的,解決了第一種方案的弊端,擴充性什麼的都很好,是我比較推薦的,也是我們正在採用的一種方案~

當然,這不過是我的一種實現思路,如果你有關於這方面更好的解決方案記得及時艾特我,我已經迫不及待的等待學習了~

小程式中 icon 顏色自定義解決方案

相關文章