react-native 生成iconfont.json配置文件

作者: 大猪大猪 | 来源:发表于2018-05-19 00:49 被阅读63次

我们要在react-native中使用react-native-vector-icons的时候,需要自己生成iconfont.json配置文件,生成对应的unicode配置文件,几个图标还可以手动生成,但是几十个上百的时候就哈哈了,这里为大家写了一个nodejs生成的程序,可直接使用。

使用教程

cat buildJson.js

const fs = require('fs');
const path = require('path');

const lineReader = require('readline').createInterface({
    input: require('fs').createReadStream(path.join(__dirname,'./iconfont.svg'))
});

const names = [];
console.log('create...');
lineReader.on('line', function (line) {
    let words = line.split(' ');
    if(words[4]==='<glyph'){
        let [key,value] = [words[5].slice(12,-1),words[6].slice(11,-2)];
        if(value){
            names.push('    "'+key+'":'+value);
        }
    }
});
lineReader.on('close',function () {
    return fs.writeFile(path.resolve(__dirname, './iconfont.json'), '{\n'+names.join(',\n')+'\n}', function (err) {
        if (err) {
            throw new Error(err)
        } else {
            console.log('create successe.');
        }
    })
});

运行

node buildJson.js

生成后的iconfont.json文件内容格式如下

{
    "emotion":58953,
    "voice":59119,
    "send-up":58990,
    "copy":60061,
    "more":58900,
    "male":58932,
    "female":58934,
    "camera-bar":59369,
    "picture":58948,
    "chat":59208,
    "translate-lang":58986,
    "add":58916,
    "speak":58885,
    "send":58931,
    "chat-settings":58907,
    "translate":58901,
    "translate-left-down":58891,
    "translate-right-down":58894,
    "translate-left-up":58895,
    "translate-right-up":58897
}

相关文章

网友评论

    本文标题:react-native 生成iconfont.json配置文件

    本文链接:https://www.haomeiwen.com/subject/cxqmdftx.html