小程序中 icon 颜色自定义解决方案

背景

随着公司业务的发展,现在的产品真的是越来越 体(sang)贴(xin)用(bing)户(kuang) 了,刚好前阵子准备做一个后台可以让用用户 DIY 自己的小程序,不小心看到了 某网 提供了一套图标库,用户可以自主选择,并且可以进行颜色配置的时候,我就感觉事情不妙~

当时脑子一热,桌子一拍,我们也需要提供这样的功能,而且要做的比他们还强大...

作为开发的我默默的留下了这张图~

我们知道小程序里面是不支持类似 ttf woff 这类字体的,直接引入编译都过不去~

这个就是要告诉我们,我们不能像 H5 那样直接把字体下载下来然后引入到样式中,这条道路是行不通了,只能重新找找方案了~

嗯,我也不 藏着掖着了 , 接下来给大家 安利 2 中解决方案~

解决方案

方案一

利用小程序中不支持 ttf 字体,却支持 base64 的特点,我们可以通过将 ttf 字体转码成 base64 ,再通过 iconfont 的特点,通过设置字体大小和颜色来达到目的。

其具体实现如下:

  • 1.我们可以先去阿里提供的图标库 官网 下载我们需要的图标,一般来说都够用了,这里我随便好了 3 个作为测试用 , 点击 下载代码 将代码下载到本地~

  • 2.解压我们刚才下载的包,会有以下这些文件,但对于我们来说,会用到的只有 iconfont.cssiconfont.ttf 这两个文件,

  • 3.我们要对 iconfont.ttf 这个文件进行转码,将它转成 base64 格式,刚好 这个网站 就提供了这个功能(pass:这个网站可能会有点慢,可以的话代理加个速),按照下图的步骤,分别上传 ttf 文件,选择配置,点击转换。

  • 4.转换完成如下图所示,废话不多说,直接下载下来解压,我们需要的是里面 stylesheet.css 这个文件

  • 5.我们在小程序里的静态资源文件夹里新建一个叫 iconfont.wxss 样式文件,将 上面解压出来的 stylesheet.css 里面的内容全部拷贝到 iconfont.wxss 中,另外,还记得我们上面说的 iconfont.css 吗?这时候就要派上用场了,我们打开 iconfont.css 这个样式文件,将下面的样式,也就是我用红框标注的内容复制到 iconfont.wxss 文件里,注意上面的不需要复制!!

到此为止,准备工作算是已经完成了,这时候你的 iconfont.wxss 应该类似下面这样

iconfont.wxss

@font-face {
    font-family: 'iconfont';
    src: url(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AAHAAAAAQACAFsBAgEDAQQDbWFwB3N1Y2Nlc3MDYXRtAAAAAf//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 写对就没什么问题了,

    
    "container">
        'block'>
            'iconfont icon-map'>
            地图
        
        'block'>
            'iconfont icon-success'>
            成功
        
        'block'>
            'iconfont icon-atm'>
            atm
        
    
    复制代码

    怎么样,成功了吧!

    什么? 你要换颜色,难不倒我的,只要改变对应的 color 就可以了~

    
    "container">
        'block'>
            'iconfont icon-map' style='color : red'>
            地图
        
        'block'>
            'iconfont icon-success' style='color : green'>
            成功
        
        'block'>
            'iconfont icon-atm' style='color : blue'>
            atm
        
    
    复制代码

嗯,是不是可以插会儿腰了~

且慢,刚不是说两种方案么,怎么到这里就叉起腰来了,还没完呢,等会嘚瑟哈!!

方案二

刚介绍了方案一,确实是一种选择,我们只需约定好样式,类名,后端将颜色什么的返回给我们就好了~

但是这种方案存在弊端,不够灵活,如果我们要更新我们的图标库,那么我们上面的流程又得走一遍,如果后面图标库的类型变多,那么我们转码后的文件会随之变大,这对于小程序来说是致命的,因为做小程序都知道,我们都在时刻想着如何用最少的代码发挥最大的价值!

所以,这种方案有一定的局限性,因此才出了第二套方案~

第二套方案的思路是借助 svg 矢量图具有不失真和可以自主填充颜色的特点,通过 fill 属性,用户可以自定义填充颜色,但是小程序里面不支持 svg 标签的,我们通过颜色选择器将用户选择好的颜色填充到 svg 里,在通过转码将换过颜色的 svg 转成 base64 编码发给小程序,小程序可以直接用在 image 标签中的 src 属性上~

我们一起来试试~

  • 1.我们还是先去阿里提供的图标库 官网 下载我们需要的图标,这次跟第一次有些不一样,这次我们要选择下载素材,选择 svg~

  • 2.将下载好的 svg 源代码直接放在我们的图标库里面,选择某个图标之后,通过颜色选择器我们可以通过 js 设置对应的 svg 图标的 fill 属性,动态的给图标填充不同的颜色,差不多就跟下面的效果有点相似~

  • 3.接下来可以将换过颜色的 svg 通过 svgbase64 的插件将其转换成 base64 编码,这里为了方便,我直接偷懒,在线转换了,对应的 base64 编码如下:

data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyBjbGFzcz0iaWNvbiIgd2lkdGg9IjMycHgiIGhlaWdodD0iMzIuMDBweCIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIGZpbGw9InJlZCIgZD0iTTUxMy41Nzc4MDcgMTAxOC40ODg0MjVhMjQuNTcyMjI0IDI0LjU3MjIyNCAwIDAgMS0yMC4xOTMwNjYtMTAuNTY4NTQ0TDE5Mi4wMDIwNDggNTczLjM1NDI4YTI0LjY4MDc1NiAyNC42ODA3NTYgMCAwIDEtMC45Mzc4NzktMS40NTgwMTIgMzczLjg4MTk2NSAzNzMuODgxOTY1IDAgMCAxLTM0LjA3Njk1Mi03NS4zNjYxNTFjLTEyLjA3Nzc1MS0zNy4zMTE0MDUtMTguMTk4NTM4LTc2LjE5NjUyMi0xOC4xOTg1MzctMTE1LjU4OTQ4NiAwLTIwNi42NjQwODYgMTY4LjEzMzIzMy0zNzQuNzk4MzQzI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4wMTEzNTkgNTExLjcwODE5MyAxNzQuMDExMzU5ek01MzYuMjk4ODQ3IDU0Ni4zMDAxNThjLTE0LjEzNzgwOSAwLTI1LjU5NzEzMy0xMS40NTkzMjUtMjUuNTk3MTMzLTI1LjU5NzEzM2wwLTc2LjczNzEzM2MwLTE0LjEzNjc4NSAxMS40NTkzMjUtMjUuNTk3MTMzIDI1LjU5NzEzMy0yNS41OTcxMzNzMjUuNTk3MTMzIDExLjQ2MDM0OCAyNS41OTcxMzMgMjUuNTk3MTMzbDAgNzYuNzM3MTMzQzU2MS44OTU5OCA1MzQuODQwODM0IDU1MC40MzU2MzEgNTQ2LjMwMDE1OCA1MzYuMjk4ODQ3IDU0Ni4zMDAxNTh6TTcyMy42Njk4NjEgNTQ2LjMwMDE1OGMtMTQuMTM3ODA5IDAtMjUuNTk3MTMzLTExLjQ1OTMyNS0yNS41OTcxMzMtMjUuNTk3MTMzbDAtNzYuNzM3MTMzYzAtMTQuMTM2Nzg1IDExLjQ1OTMyNS0yNS41OTcxMzMgMjUuNTk3MTMzLTI1LjU5NzEzM3MyNS41OTcxMzMgMTEuNDYwMzQ4IDI1LjU5NzEzMyAyNS41OTcxMzNsMCA3Ni43MzcxMzNDNzQ5LjI2Njk5NCA1MzQuODQwODM0IDczNy44MDY2NDYgNTQ2LjMwMDE1OCA3MjMuNjY5ODYxIDU0Ni4zMDAxNTh6IiAvPjwvc3ZnPg==
复制代码
  • 4.接着我们只需要把对应的 base64 编码发给小程序端,小程序直接放在 image 组件中的 src 属性即可,对于样式来说,可以直接修改 image 的样式来控制 icon 样式,效果如下:

ok~ 还算是比较圆满的完成了任务~

这下产品总不会刁难我了吧~

结语

到此,我们已经 get 到了两种小程序中实现 icon 自定义颜色的解决方案,对比发现,第一种方案的特点是实现起来比较简单,步骤什么的也都还ok,就是拓展性不强,每次改动都需要重新再来一遍,而且对于比较多的图标来说小程序空间占用较大,有一定的局限性,而第二种方案相比于第一种来说虽然在实现过程中稍微繁琐了一些,但是带来的效果是显而易见的,解决了第一种方案的弊端,拓展性什么的都很好,是我比较推荐的,也是我们正在采用的一种方案~

当然,这不过是我的一种实现思路,如果你有关于这方面更好的解决方案记得及时艾特我,我已经迫不及待的等待学习了~

你可能感兴趣的:(小程序中 icon 颜色自定义解决方案)