我正在尝试使用gulp-iconfont从一组svg图像构建图标字体。
我已经创建了我的gulp任务,运行它时没有任何错误。但是我也不能得到每个图标的代码,这是我在css伪元素上使用图标所需要的。
控制台输出显示了unicode所在位置的奇怪字符:

这是我的任务:
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/'));
});当svg选项设置为true时,我可以在我的appendUnicode文件名中看到它,例如uEA02-calendar.svg。
但是,如果我尝试在我的css文件上使用它:
.calendar:before {
content: "uEA02";
speak: none;
font-style: normal;
font-weight: normal;
font-family: "icon-font"; }我看到的是文本uEA02而不是我的图标。我已经检查过了,字体正在加载,我不知道这里会遗漏什么?
发布于 2016-04-05 07:27:45
我通常将gulp iconfont与gulp-iconfont-css配对。这个额外的包导出一个样式表,其中适当的实体绑定到一个类。您可以导出预处理的css或普通css。
这是我的任务。
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;发布于 2016-04-26 23:37:06
您只需使用反斜杠(\)对unicode字符串进行转义。在你的CSS中只需写下:
.calendar:before {
content: "\EA02";
speak: none;
font-style: normal;
font-weight: normal;
font-family: "icon-font";
}发布于 2019-06-28 05:14:26
您需要在传递给"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);
})此解决方案无法归功于此-在this post中找到了答案
https://stackoverflow.com/questions/33297107
复制相似问题