• gulp iconfont


    参考如下网站

    https://github.com/hjzheng/CUF_meeting_knowledge_share/tree/master/2015-7-24/gulp-test-iconfont

    gulpfile.js

    var gulp = require('gulp');
    var iconfont = require('gulp-iconfont');
    
    var iconfontCss = require('gulp-iconfont-css');
    
    var template = require('gulp-template');
    var fs = require('fs');
    
    var icons = fs.readdirSync('src/style/svg');
    
    icons = icons.map(function(icon) {
        return icon.replace(/.w+$/, '');
    });
    
    var cssName = 'iconfont'; //生成的css名称
    gulp.task('icon', function() {
        let dirname = 'src/style';
        return gulp
            .src([`${dirname}/svg/*.svg`])
    
            .pipe(
                iconfontCss({
                    fontName: 'iconfont', // iconfont.css的font-family值
                    path: `${dirname}/template/iconfont.template.less`, //css模版文件
                    targetPath: `../${cssName}.css`, //css生成文件
                    fontPath: 'fonts/' //iconfont.template.less文件中的fontPath
                })
            )
            .pipe(
                iconfont({
                    fontName: 'iconfont',
                    formats: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
                    normalize: true
                })
            )
            .pipe(gulp.dest(`${dirname}/fonts`)); //svg的字体文件存放位置
    });
    
    gulp.task('example', function() {
        let dirname = 'src/style';
        gulp.src(`${dirname}/example/index.html`) //样式例子文件
            .pipe(template({ icons: icons, cssName: cssName }))
            .pipe(gulp.dest(`${dirname}`)); //样式例子文件存放位置
    });
    gulp.task('default', ['icon', 'example']);

    iconfont-template.less

    @font-face {
        font-family: "<%= fontName %>";
        src: url('<%= fontPath %><%= fontName %>.eot');
        src: url('<%= fontPath %><%= fontName %>.eot?#iefix') format('eot'),
            url('<%= fontPath %><%= fontName %>.woff') format('woff'),
            url('<%= fontPath %><%= fontName %>.ttf') format('truetype'),
            url('<%= fontPath %><%= fontName %>.svg#<%= fontName %>') format('svg');
        font-weight: normal;
        font-style: normal;
    }
    
    .icon:before {
        font-family: "<%= fontName %>";
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        font-style: normal;
        font-variant: normal;
        font-weight: normal;
        /* speak: none; only necessary if not using the private unicode range (firstGlyph option) */
        text-decoration: none;
        text-transform: none;
    }
    
    <% _.each(glyphs, function(glyph) { %>
    .icon-<%= glyph.fileName %>:before {
        /* content: "<%= JSON.stringify(glyph) %>";*/
        content: "<%= glyph.codePoint %>";
    
    }
    <% }); %>

    example/index.html

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>icon font test</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="<%= cssName%>.css">
        <style>
            .icon_lists li{
              float:left;
              width: 120px;
              height:170px;
              text-align: center;
              list-style: none;
            }
            .icon_lists .icon{
              font-size: 42px;
              margin: 10px 0;
              color:#333;
              -webkit-transition: font-size 0.25s ease-out 0s;
              -moz-transition: font-size 0.25s ease-out 0s;
              transition: font-size 0.25s ease-out 0s;
    
            }
            .icon_lists .icon:hover{
              font-size: 120px;
            }
            .name {
              font-size: 16px;
            }
    
            .clear:after {
              content: '20';
              display: block;
              height: 0;
              clear: both;
            }
        </style>
    </head>
    
    <body>
        <h2>Icon Font</h2>
        <ul class="icon_lists clear">
        <% _.each(icons, function(icon) { %>
           <li class="icon icon-<%= icon %>">
              <div class="name"><%= icon%></div>
           </li>
        <% }); %>
        </ul>
       <div>
         <h2>Use Icon Font</h2>
         <pre>
          &lt;span class=&quot;icon icon-add&quot;&gt;&lt;/span&gt;
         </pre> 
       </div>    
    </body>
    </html>

    执行语句

    gulp icon

    gulp example

    gulp.task('example', function() {....
    中的task值相同

    gulpfile.js文件中增加
      gulp.task('default', ['icon', 'example']);
     就可以直接gulp生成font,css ,html





    =====缺点
    生成的font字体 1.只能是黑白色调, 2.svg图片中如果有线条重合,就会被重叠消除为白色【这个问题在
    icomoon网站中也存在
    =====总结
    https://icomoon.io/app/ 这个网站生成font字体,还是不错的


    【第一种缺点是没有颜色】

    第二种生成iconfont方式【可以有颜色】

    gulpfile.js
    var gulp = require('gulp');
    var rename = require('gulp-rename');
    var iconfont = require('gulp-iconfont');
    var consolidate = require('gulp-consolidate');
    
    var template = require('gulp-template');
    var fs = require('fs');
    
    var icons = fs.readdirSync('src/style/svg');
    
    icons = icons.map(function(icon) {
        return icon.replace(/.w+$/, '');
    });
    
    gulp.task('icon', function() {
        let dirname = 'src/style';
        return gulp
            .src([`${dirname}/svg/*.svg`])
            .pipe(
                iconfont({
                    fontName: 'iconfont1',
                    formats: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
                    normalize: true,
                    options: {
                        fixedWidth: false,
                        normalize: false,
                        // fontHeight: 512,
                        // descent: -32
                    },
                    // prependUnicode: true // 会修改掉svg的名字
                })
            )
            .on('glyphs', function(glyphs, options) {
                gulp.src(`${dirname}/iconfont.template.1.less`)
                    .pipe(
                        consolidate('lodash', {
                            glyphs: glyphs.map(function(glyph) {
                                glyph.unicode = glyph.unicode[0]
                                    .charCodeAt(0)
                                    .toString(16)
                                    .toUpperCase(); // unicode是16进制的
                                return glyph;
                            }),
                            fontName: options.fontName,
                            // glyphs: glyphs,
                            fontPath: 'fonts/'
                        })
                    )
                    .pipe(rename(`${options.fontName}.css`))
                    .pipe(gulp.dest(dirname));
            })
            .pipe(gulp.dest(`${dirname}/fonts`));
    });
    
    gulp.task('example', function() {
        let dirname = 'src/style';
        gulp.src(`${dirname}/example/index.1.html`)
            .pipe(template({ icons: icons }))
            .pipe(gulp.dest(`${dirname}`));
    });
    
    gulp.task('default', ['icon', 'example']);
    iconfont-template.less
    中循环语句修改
    <% _.each(glyphs, function(glyph) { %>
    .icon-<%= glyph.name %>:before {
        /* content: "<%= JSON.stringify(glyph) %>";*/
        content: "<%= glyph.unicode %>";
        <%= glyph.color?'color:'+glyph.color :null %>
    }
    <% }); %>





  • 相关阅读:
    Android sdk + PhoneGap 配置
    一个解析url参数方法
    开始写笔记了~~
    Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA
    Codevs 3304 水果姐逛水果街Ⅰ 线段树
    Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理
    Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
    Bzoj 2453: 维护队列 && Bzoj 2120: 数颜色 分块,bitset
    Uoj #131. 【NOI2015】品酒大会 后缀数组,并查集
    Tyvj P1463 智商问题 分块
  • 原文地址:https://www.cnblogs.com/jcz1206/p/9797641.html
Copyright © 2020-2023  润新知