简体   繁体   English

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

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

I'm trying to use gulp-iconfont to build an icon font from a set of svg images. 我正在尝试使用gulp-iconfont从一组svg图像构建图标字体。

I've created my gulp task and there're no errors when I run it. 我已经创建了我的gulp任务,当我运行它时没有错误。 But neither can I get the code for each icon, which is what I need to use the icons on css pseudoelements. 但我也无法获得每个图标的代码,这就是我需要使用css pseudoelements上的图标。

The console output shows strange characters where the unicode is supposed to be: 控制台输出显示unicode应该是的奇怪字符:

gulp-iconfont输出

Here's my gulp task: 这是我的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/'));
});

As the appendUnicode option is set to true, I can see it at the beggining of my svg file name, for example uEA02-calendar.svg . 由于appendUnicode选项设置为true,我可以在我的svg文件名的uEA02-calendar.svg看到它,例如uEA02-calendar.svg

However, if I try to use it on my css file: 但是,如果我尝试在我的css文件中使用它:

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

what I see is the text uEA02 instead of my icon. 我看到的是文字uEA02而不是我的图标。 I've checked and the font is loading, I don't know what could I be missing here? 我已经检查过了,字体正在加载,我不知道我在这里可以找到什么?

I usually pair gulp-iconfont with gulp-iconfont-css. 我通常将gulp-iconfont与gulp-iconfont-css配对。 This additional package exports a stylesheet with the appropriate entities binded to a class. 此附加包导出样式表,其中包含绑定到类的相应实体。 You can export pre-processed css or vanilla css. 您可以导出预处理的CSS或香草css。

Here's my gulp task. 这是我的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;

You simply need to escape the unicode string with a backslash ( \\ ). 您只需要使用反斜杠( \\ )转义unicode字符串。 In your CSS just write: 在你的CSS中写道:

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

You need to reformat the unicode from within the function you're passing to the "on glyphs" event. 您需要从传递给“on glyphs”事件的函数中重新格式化unicode。 Otherwise the unicode will be lost in templating. 否则unicode将在模板中丢失。

I'd suggest something like this: 我建议这样的事情:

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

Can't take credit for this solution - found the answer in this post 不能归功于这个解决方案 - 在这篇文章中找到答案

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

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