繁体   English   中英

使用gulp-iconfont时无法获取字形代码

[英]Can't get the glyph code when using gulp-iconfont

我正在尝试使用gulp-iconfont从一组svg图像构建图标字体。

我已经创建了我的gulp任务,当我运行它时没有错误。 但我也无法获得每个图标的代码,这就是我需要使用css pseudoelements上的图标。

控制台输出显示unicode应该是的奇怪字符:

gulp-iconfont输出

这是我的gulp任务:

gulp.task('iconfont', function(){
    gulp.src(['assets/icons/*.svg'])
        .pipe(iconfont({
            fontName: 'icon-font', // required
            appendUnicode: true, // recommended option
            normalize: true,
            centerHorizontally: true,
            fontHeight: 100,
            formats: ['ttf', 'eot', 'woff'], 
        }))
        .on('glyphs', function(glyphs, options) {
            console.log(glyphs, options);
      })
    .pipe(gulp.dest('assets/fonts/'));
});

由于appendUnicode选项设置为true,我可以在我的svg文件名的uEA02-calendar.svg看到它,例如uEA02-calendar.svg

但是,如果我尝试在我的css文件中使用它:

.calendar:before {
  content: "uEA02";
  speak: none;
  font-style: normal;
  font-weight: normal;
  font-family: "icon-font"; }

我看到的是文字uEA02而不是我的图标。 我已经检查过了,字体正在加载,我不知道我在这里可以找到什么?

我通常将gulp-iconfont与gulp-iconfont-css配对。 此附加包导出样式表,其中包含绑定到类的相应实体。 您可以导出预处理的CSS或香草css。

这是我的gulp任务。

    var iconfont = require('gulp-iconfont');
    var iconfontCss = require('gulp-iconfont-css');

    var glyphFontName = 'icon';

    var stream = gulp.src('resources/assets/glyph/*.svg')
        .pipe(iconfontCss({
            fontName: glyphFontName,
            path: 'node_modules/gulp-iconfont-css/templates/_icons.scss',
            targetPath: '../../resources/sass/generated/' + glyphFontName + '.scss',
            fontPath: '../fonts/',
            cssClass: 'i',
            centerHorizontally: true
        }))
        .pipe(iconfont({
            fontName: glyphFontName,
            formats: [
                'ttf',
                'eot',
                'woff',
                'woff2'
            ],
        }))
        .pipe(gulp.dest('public/fonts/'));

    return stream;

您只需要使用反斜杠( \\ )转义unicode字符串。 在你的CSS中写道:

.calendar:before {
  content: "\EA02";
  speak: none;
  font-style: normal;
  font-weight: normal;
  font-family: "icon-font";
}

您需要从传递给“on glyphs”事件的函数中重新格式化unicode。 否则unicode将在模板中丢失。

我建议这样的事情:

.on('glyphs', function(glyphs, options) {
    glyphs = glyphs.map((glyph) => {
        ...glyph,
        unicode: glyph.unicode[0].codePointAt(0).toString(16).toUpperCase()
    });
    console.log(glyphs, options);
})

不能归功于这个解决方案 - 在这篇文章中找到答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM