{"id":15067184,"url":"https://github.com/iconfont-cli/react-native-iconfont-cli","last_synced_at":"2025-04-12T21:24:27.035Z","repository":{"id":35079334,"uuid":"204236560","full_name":"iconfont-cli/react-native-iconfont-cli","owner":"iconfont-cli","description":"把iconfont.cn的图标转换成标准RN组件，不依赖字体，支持多色彩，支持热更新","archived":false,"fork":false,"pushed_at":"2023-01-04T08:14:49.000Z","size":655,"stargazers_count":470,"open_issues_count":18,"forks_count":64,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-04T00:37:24.700Z","etag":null,"topics":["iconfont","react-native","react-native-icon","react-native-iconfont"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iconfont-cli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-25T02:33:51.000Z","updated_at":"2025-03-21T06:07:04.000Z","dependencies_parsed_at":"2023-01-15T13:30:38.076Z","dependency_job_id":null,"html_url":"https://github.com/iconfont-cli/react-native-iconfont-cli","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconfont-cli%2Freact-native-iconfont-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconfont-cli%2Freact-native-iconfont-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconfont-cli%2Freact-native-iconfont-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iconfont-cli%2Freact-native-iconfont-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iconfont-cli","download_url":"https://codeload.github.com/iconfont-cli/react-native-iconfont-cli/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248633023,"owners_count":21136790,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["iconfont","react-native","react-native-icon","react-native-iconfont"],"created_at":"2024-09-25T01:17:43.030Z","updated_at":"2025-04-12T21:24:27.008Z","avatar_url":"https://github.com/iconfont-cli.png","language":"TypeScript","readme":"## react-native-iconfont-cli\n用纯JS把iconfont.cn的图标转换成RN组件，不依赖字体，支持多色彩，支持热更新\n\n![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/icons.png?raw=true)\n\n## 痛点\n\n通常地，当我们想在RN里使用iconfont，我们可能会借助`react-native-vector-icons`导入ttf字体文件，或者直接手动下载各个svg文件到本地，然后单个使用。\n\n使用ttf字体有一个弊端，就是每次更新图标，都要相应的更新ttf文件，然后再次打包发布APP。而且ttf不支持多种色彩的图标，导致所有图标都是单色。如果你是借助`react-native-vector-icons`，该库内置了10多套ttf文件，合起来有`2M`左右；你可能用不到它们，但是它们仍然会被打包进你的APP里。\n\n下载svg到本地也不方便，因为你需要额外维护一份图标字体，有可能造成线上和本地的图标不一致问题。而且如果你想动态地改变svg的渲染色彩，基本上是不可能的，只能渲染原图的颜色。\n\n--------\n\n为了解决这些问题，我用纯Javascript实现iconfont到React组件的转换操作，**不需要依赖ttf字体文件**，不需要手动下载图标到本地。\n\n## 特性\n\n1、纯组件，不依赖字体，体积小\n\u003cbr /\u003e\n2、支持渲染多色彩图标，支持自定义颜色\n\u003cbr /\u003e\n3、支持热更新\n\u003cbr /\u003e\n4、自动化生成图标组件，支持es6和typescript两种文件格式\n\n## Step 1\n安装插件\n```bash\n# Yarn\nyarn add react-native-svg\nyarn add react-native-iconfont-cli --dev\n\n# Npm\nnpm install react-native-svg\nnpm install react-native-iconfont-cli --save-dev\n```\n\n# Step 2\n静态链接。请注意您使用的React-Native版本号\n```bash\n# RN \u003c 0.60\nreact-native link react-native-svg\n\n# RN \u003e= 0.60\ncd ios \u0026\u0026 pod install\n```\n\n# Step 3\n生成配置文件\n```bash\nnpx iconfont-init\n```\n此时项目根目录会生成一个`iconfont.json`的文件，内容如下：\n```json\n{\n    \"symbol_url\": \"请参考README.md，复制官网提供的JS链接\",\n    \"use_typescript\": false,\n    \"save_dir\": \"./src/iconfont\",\n    \"trim_icon_prefix\": \"icon\",\n    \"default_icon_size\": 18,\n    \"local_svgs\": \"./localSvgs\"\n}\n```\n### 配置参数说明：\n### symbol_url\n请直接复制[iconfont](http://iconfont.cn)官网提供的项目链接。请务必看清是`.js`后缀而不是.css后缀。如果你现在还没有创建iconfont的仓库，那么可以填入这个链接去测试：`http://at.alicdn.com/t/font_1373348_ghk94ooopqr.js`\n\n\u003cbr /\u003e\n\n![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/symbol-url.png?raw=true)\n\n### use_typescript\n如果您的项目使用Typescript编写，请设置为true。这个选项将决定生成的图标组件是`.tsx`还是`.js`后缀。\n\n当该值为false时，我们会为您的图标生成`.js`和`.d.ts`两种文件，以便您能享受到最好的开发体验。\n\n### save_dir\n根据iconfont图标生成的组件存放的位置。每次生成组件之前，该文件夹都会被清空。\n\n### trim_icon_prefix\n如果你的图标有通用的前缀，而你在使用的时候又不想重复去写，那么可以通过这种配置这个选项把前缀统一去掉。\n\n### default_icon_size\n我们将为每个生成的图标组件加入默认的字体大小，当然，你也可以通过传入props的方式改变这个size值。\n\n### local_svgs\n本地 svg 的路径, 配置此项后在路径中添加 svg 文件即可。支持渐变图标（iconfont官网不能上传渐变图标）。\n\n**注 1: 暂不支持 `color` 参数**\n\u003cbr\u003e\n**注 2: 如果启用该功能，请确保`react-native-svg`的版本`\u003e=9.13.0`**\n\n\n# Step 4\n开始生成React-Native标准组件\n```bash\nnpx iconfont-rn\n```\n\n生成后查看您设置的保存目录中是否含有所有的图标，你可以参考[snapshots目录](https://github.com/iconfont-cli/react-native-iconfont-cli/tree/master/snapshots)的快照文件，以区分不同模式下的图标结构。\n\n# 使用\n\u003cbr /\u003e\n现在我们提供了两种引入方式供您选择：\n\n1、使用汇总`Icon`组件：\n```typescript jsx\nimport IconFont from '../src/iconfont';\n\nexport const App = () =\u003e {\n  return (\n    \u003cView\u003e\n      \u003cIconFont name=\"alipay\" size={20} /\u003e\n      \u003cIconFont name=\"wechat\" /\u003e\n    \u003c/View\u003e\n  );\n};\n```\n\n2、使用单个图标。这样可以避免没用到的图标也打包进App：\n\n```typescript jsx\nimport IconAlipay from '../src/iconfont/IconAlipay';\nimport IconWechat from '../src/iconfont/IconWechat';\n\nexport const App = () =\u003e {\n  return (\n    \u003cView\u003e\n      \u003cIconAlipay size={20} /\u003e\n      \u003cIconWechat /\u003e\n    \u003c/View\u003e\n  );\n};\n```\n\n### 图标尺寸\n根据配置`default_icon_size`，每个图标都会有一个默认的尺寸，你可以随时覆盖。\n```typescript jsx\n\u003cIconFont name=\"alipay\" size={20} /\u003e\n```\n![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/default-color-icon.png?raw=true)\n### 图标单色\n单色图标，如果不指定颜色值，图标将渲染原本的颜色。如果你想设置为其他的颜色，那么设置一个你想要的颜色即可。\n\n**注意：如果你在props传入的color是字符串而不是数组，那么即使原本是多色彩的图标，也会变成单色图标。**\n\n```typescript jsx\n\u003cIconFont name=\"alipay\" color=\"green\" /\u003e\n```\n![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/one-color-icon.png?raw=true)\n\n### 图标多色彩\n多色彩的图标，如果不指定颜色值，图标将渲染原本的多色彩。如果你想设置为其他的颜色，那么设置一组你想要的颜色即可\n```typescript jsx\n\u003cIconFont name=\"alipay\" color={['green', 'orange']} /\u003e\n```\n颜色组的数量以及排序，需要根据当前图标的信息来确定。您需要进入图标组件中查看并得出结论。\n\n\n![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/multi-color-icon.png?raw=true)\n\n# 更新图标\n当您在iconfont.cn中的图标有变更时，只需更改配置`symbol_url`，然后再次执行`Step 4`即可生成最新的图标组件\n```bash\n# 修改 symbol_url 配置后执行：\nnpx iconfont-rn\n```\n\n\n欢迎使用，并给我一些反馈和建议，让这个库做的更好\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficonfont-cli%2Freact-native-iconfont-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficonfont-cli%2Freact-native-iconfont-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficonfont-cli%2Freact-native-iconfont-cli/lists"}