Skip to content

Commit 3d4e026

Browse files
Grewerfwh1990
authored andcommitted
feat: 添加库,可以供自己使用
1 parent 30ed226 commit 3d4e026

File tree

3 files changed

+92
-170
lines changed

3 files changed

+92
-170
lines changed

README.md

Lines changed: 5 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,18 @@
11
## react-native-iconfont-cli
2-
用纯JS把iconfont.cn的图标转换成RN组件,不依赖字体,支持多色彩,支持热更新
32

4-
![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/icons.png?raw=true)
3+
fork react-native-iconfont-cli 并且添加了 本地 svg 的支持
54

6-
## 痛点
5+
原库的使用: https://github.com/iconfont-cli/react-native-iconfont-cli
76

8-
通常地,当我们想在RN里使用iconfont,我们可能会借助`react-native-vector-icons`导入ttf字体文件,或者直接手动下载各个svg文件到本地,然后单个使用。
7+
添加了本地 svg 支持之后的配置文件:
98

10-
使用ttf字体有一个弊端,就是每次更新图标,都要相应的更新ttf文件,然后再次打包发布APP。而且ttf不支持多种色彩的图标,导致所有图标都是单色。如果你是借助`react-native-vector-icons`,该库内置了10多套ttf文件,合起来有`2M`左右;你可能用不到它们,但是它们仍然会被打包进你的APP里。
11-
12-
下载svg到本地也不方便,因为你需要额外维护一份图标字体,有可能造成线上和本地的图标不一致问题。而且如果你想动态地改变svg的渲染色彩,基本上是不可能的,只能渲染原图的颜色。
13-
14-
--------
15-
16-
为了解决这些问题,我用纯Javascript实现iconfont到React组件的转换操作,**不需要依赖ttf字体文件**,不需要手动下载图标到本地。
17-
18-
## 特性
19-
20-
1、纯组件,不依赖字体,体积小
21-
<br />
22-
2、支持渲染多色彩图标,支持自定义颜色
23-
<br />
24-
3、支持热更新
25-
<br />
26-
4、自动化生成图标组件,支持es6和typescript两种文件格式
27-
28-
## Step 1
29-
安装插件
30-
```bash
31-
# Yarn
32-
yarn add react-native-svg
33-
yarn add react-native-iconfont-cli --dev
34-
35-
# Npm
36-
npm install react-native-svg
37-
npm install react-native-iconfont-cli --save-dev
38-
```
39-
40-
# Step 2
41-
静态链接。请注意您使用的React-Native版本号
42-
```bash
43-
# RN < 0.60
44-
react-native link react-native-svg
45-
46-
# RN >= 0.60
47-
cd ios && pod install
48-
```
49-
50-
# Step 3
51-
生成配置文件
52-
```bash
53-
npx iconfont-init
549
```
55-
此时项目根目录会生成一个`iconfont.json`的文件,内容如下:
56-
```json
5710
{
5811
"symbol_url": "请参考README.md,复制官网提供的JS链接",
5912
"use_typescript": false,
6013
"save_dir": "./src/iconfont",
6114
"trim_icon_prefix": "icon",
62-
"default_icon_size": 18
15+
"default_icon_size": 18,
16+
"local_dir": "./locals" // 唯一不同点
6317
}
6418
```
65-
### 配置参数说明:
66-
### symbol_url
67-
请直接复制[iconfont](http://iconfont.cn)官网提供的项目链接。请务必看清是`.js`后缀而不是.css后缀。如果你现在还没有创建iconfont的仓库,那么可以填入这个链接去测试:`http://at.alicdn.com/t/font_1373348_ghk94ooopqr.js`
68-
69-
<br />
70-
71-
![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/symbol-url.png?raw=true)
72-
73-
### use_typescript
74-
如果您的项目使用Typescript编写,请设置为true。这个选项将决定生成的图标组件是`.tsx`还是`.js`后缀。
75-
76-
当该值为false时,我们会为您的图标生成`.js``.d.ts`两种文件,以便您能享受到最好的开发体验。
77-
78-
### save_dir
79-
根据iconfont图标生成的组件存放的位置。每次生成组件之前,该文件夹都会被清空。
80-
81-
### trim_icon_prefix
82-
如果你的图标有通用的前缀,而你在使用的时候又不想重复去写,那么可以通过这种配置这个选项把前缀统一去掉。
83-
84-
### default_icon_size
85-
我们将为每个生成的图标组件加入默认的字体大小,当然,你也可以通过传入props的方式改变这个size值。
86-
87-
# Step 4
88-
开始生成React-Native标准组件
89-
```bash
90-
npx iconfont-rn
91-
```
92-
93-
生成后查看您设置的保存目录中是否含有所有的图标,你可以参考[snapshots目录](https://github.com/iconfont-cli/react-native-iconfont-cli/tree/master/snapshots)的快照文件,以区分不同模式下的图标结构。
94-
95-
# 使用
96-
<br />
97-
现在我们提供了两种引入方式供您选择:
98-
99-
1、使用汇总`Icon`组件:
100-
```typescript jsx
101-
import IconFont from '../src/iconfont';
102-
103-
export const App = () => {
104-
return (
105-
<View>
106-
<IconFont name="alipay" size={20} />
107-
<IconFont name="wechat" />
108-
</View>
109-
);
110-
};
111-
```
112-
113-
2、使用单个图标。这样可以避免没用到的图标也打包进App:
114-
115-
```typescript jsx
116-
import IconAlipay from '../src/iconfont/IconAlipay';
117-
import IconWechat from '../src/iconfont/IconWechat';
118-
119-
export const App = () => {
120-
return (
121-
<View>
122-
<IconAlipay size={20} />
123-
<IconWechat />
124-
</View>
125-
);
126-
};
127-
```
128-
129-
### 图标尺寸
130-
根据配置`default_icon_size`,每个图标都会有一个默认的尺寸,你可以随时覆盖。
131-
```typescript jsx
132-
<IconFont name="alipay" size={20} />
133-
```
134-
![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/default-color-icon.png?raw=true)
135-
### 图标单色
136-
单色图标,如果不指定颜色值,图标将渲染原本的颜色。如果你想设置为其他的颜色,那么设置一个你想要的颜色即可。
137-
138-
**注意:如果你在props传入的color是字符串而不是数组,那么即使原本是多色彩的图标,也会变成单色图标。**
139-
140-
```typescript jsx
141-
<IconFont name="alipay" color="green" />
142-
```
143-
![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/one-color-icon.png?raw=true)
144-
145-
### 图标多色彩
146-
多色彩的图标,如果不指定颜色值,图标将渲染原本的多色彩。如果你想设置为其他的颜色,那么设置一组你想要的颜色即可
147-
```typescript jsx
148-
<IconFont name="alipay" color={['green', 'orange']} />
149-
```
150-
颜色组的数量以及排序,需要根据当前图标的信息来确定。您需要进入图标组件中查看并得出结论。
151-
152-
153-
![](https://github.com/fwh1990/react-native-iconfont-cli/blob/master/images/multi-color-icon.png?raw=true)
154-
155-
# 更新图标
156-
当您在iconfont.cn中的图标有变更时,只需更改配置`symbol_url`,然后再次执行`Step 4`即可生成最新的图标组件
157-
```bash
158-
# 修改 symbol_url 配置后执行:
159-
npx iconfont-rn
160-
```
161-
162-
163-
# 扩展
164-
|平台||
165-
|----|---|
166-
|小程序|[mini-program-iconfont-cli](https://github.com/iconfont-cli/mini-program-iconfont-cli)|
167-
|Taro|[taro-iconfont-cli](https://github.com/iconfont-cli/taro-iconfont-cli)|
168-
|React H5|[react-iconfont-cli](https://github.com/iconfont-cli/react-iconfont-cli)|
169-
|Flutter|[flutter-iconfont-cli](https://github.com/iconfont-cli/flutter-iconfont-cli)|
170-
|Remax|[remax-iconfont-cli](https://github.com/iconfont-cli/remax-iconfont-cli)|
171-
172-
--------
173-
174-
欢迎使用,并给我一些反馈和建议,让这个库做的更好

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "react-native-iconfont-cli",
3-
"version": "2.1.0",
2+
"name": "@grewer/react-native-iconfont-cli",
3+
"version": "2.2.0",
44
"main": "index.js",
55
"keywords": [
66
"iconfont",
@@ -10,8 +10,12 @@
1010
"icons",
1111
"iconfont.cn"
1212
],
13-
"repository": "git@github.com:iconfont-cli/react-native-iconfont-cli.git",
13+
"repository": "https://github.com/Grewer/react-native-iconfont-cli",
1414
"author": "范文华 <531362022@qq.com>",
15+
"contributors": [{
16+
"name": "grewer",
17+
"email": "grewer@grewer.cn"
18+
}],
1519
"license": "MIT",
1620
"bin": {
1721
"iconfont": "./commands/help.js",
@@ -41,7 +45,7 @@
4145
"@types/react": "^16.9.2",
4246
"react": "^16.9.0",
4347
"react-native": "^0.60.5",
44-
"react-native-svg": "^9.7.0",
48+
"react-native-svg": "^9.7.1",
4549
"ts-node": "^8.3.0",
4650
"typescript": "^3.5.3"
4751
}

yarn.lock

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,11 @@ big-integer@^1.6.7:
12011201
resolved "https://registry.npmjs.org/big-integer/-/big-integer-1.6.44.tgz#4ee9ae5f5839fc11ade338fea216b4513454a539"
12021202
integrity sha512-7MzElZPTyJ2fNvBkPxtFQ2fWIkVmuzw41+BZHSzpEq3ymB2MfeKp1+yXl/tS75xCx+WnyV+yb0kp+K1C3UNwmQ==
12031203

1204+
boolbase@^1.0.0, boolbase@~1.0.0:
1205+
version "1.0.0"
1206+
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
1207+
integrity sha1-aN/1++YMUes3cl6p4+0xDcwed24=
1208+
12041209
bplist-creator@0.0.7:
12051210
version "0.0.7"
12061211
resolved "https://registry.npmjs.org/bplist-creator/-/bplist-creator-0.0.7.tgz#37df1536092824b87c42f957b01344117372ae45"
@@ -1577,6 +1582,29 @@ cross-spawn@^6.0.0:
15771582
shebang-command "^1.2.0"
15781583
which "^1.2.9"
15791584

1585+
css-select@^2.0.2:
1586+
version "2.1.0"
1587+
resolved "https://registry.yarnpkg.com/css-select/-/css-select-2.1.0.tgz#6a34653356635934a81baca68d0255432105dbef"
1588+
integrity sha512-Dqk7LQKpwLoH3VovzZnkzegqNSuAziQyNZUcrdDM401iY+R5NkGBXGmtO05/yaXQziALuPogeG0b7UAgjnTJTQ==
1589+
dependencies:
1590+
boolbase "^1.0.0"
1591+
css-what "^3.2.1"
1592+
domutils "^1.7.0"
1593+
nth-check "^1.0.2"
1594+
1595+
css-tree@^1.0.0-alpha.37:
1596+
version "1.0.0-alpha.39"
1597+
resolved "https://registry.yarnpkg.com/css-tree/-/css-tree-1.0.0-alpha.39.tgz#2bff3ffe1bb3f776cf7eefd91ee5cba77a149eeb"
1598+
integrity sha512-7UvkEYgBAHRG9Nt980lYxjsTrCyHFN53ky3wVsDkiMdVqylqRt+Zc+jm5qw7/qyOvN2dHSYtX0e4MbCCExSvnA==
1599+
dependencies:
1600+
mdn-data "2.0.6"
1601+
source-map "^0.6.1"
1602+
1603+
css-what@^3.2.1:
1604+
version "3.3.0"
1605+
resolved "https://registry.yarnpkg.com/css-what/-/css-what-3.3.0.tgz#10fec696a9ece2e591ac772d759aacabac38cd39"
1606+
integrity sha512-pv9JPyatiPaQ6pf4OvD/dbfm0o5LviWmwxNWzblYf/1u9QZd0ihV+PMwy5jdQWQ3349kZmKEx9WXuSka2dM4cg==
1607+
15801608
csstype@^2.2.0:
15811609
version "2.6.6"
15821610
resolved "https://registry.npmjs.org/csstype/-/csstype-2.6.6.tgz#c34f8226a94bbb10c32cc0d714afdf942291fc41"
@@ -1701,11 +1729,37 @@ diff@^4.0.1:
17011729
resolved "https://registry.npmjs.org/diff/-/diff-4.0.1.tgz#0c667cb467ebbb5cea7f14f135cc2dba7780a8ff"
17021730
integrity sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==
17031731

1732+
dom-serializer@0:
1733+
version "0.2.2"
1734+
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-0.2.2.tgz#1afb81f533717175d478655debc5e332d9f9bb51"
1735+
integrity sha512-2/xPb3ORsQ42nHYiSunXkDjPLBaEj/xTwUO4B7XCZQTRk7EBtTOPaygh10YAAh2OI1Qrp6NWfpAhzswj0ydt9g==
1736+
dependencies:
1737+
domelementtype "^2.0.1"
1738+
entities "^2.0.0"
1739+
17041740
dom-walk@^0.1.0:
17051741
version "0.1.1"
17061742
resolved "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.1.tgz#672226dc74c8f799ad35307df936aba11acd6018"
17071743
integrity sha1-ZyIm3HTI95mtNTB9+TaroRrNYBg=
17081744

1745+
domelementtype@1:
1746+
version "1.3.1"
1747+
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-1.3.1.tgz#d048c44b37b0d10a7f2a3d5fee3f4333d790481f"
1748+
integrity sha512-BSKB+TSpMpFI/HOxCNr1O8aMOTZ8hT3pM3GQ0w/mWRmkhEDSFJkkyzz4XQsBV44BChwGkrDfMyjVD0eA2aFV3w==
1749+
1750+
domelementtype@^2.0.1:
1751+
version "2.0.1"
1752+
resolved "https://registry.yarnpkg.com/domelementtype/-/domelementtype-2.0.1.tgz#1f8bdfe91f5a78063274e803b4bdcedf6e94f94d"
1753+
integrity sha512-5HOHUDsYZWV8FGWN0Njbr/Rn7f/eWSQi1v7+HsUVwXgn8nWWlL64zKDkS0n8ZmQ3mlWOMuXOnR+7Nx/5tMO5AQ==
1754+
1755+
domutils@^1.7.0:
1756+
version "1.7.0"
1757+
resolved "https://registry.yarnpkg.com/domutils/-/domutils-1.7.0.tgz#56ea341e834e06e6748af7a1cb25da67ea9f8c2a"
1758+
integrity sha512-Lgd2XcJ/NjEw+7tFvfKxOzCYKZsdct5lczQ2ZaQY8Djz7pfAD3Gbp8ySJWtreII/vDlMVmxwa6pHmdxIYgttDg==
1759+
dependencies:
1760+
dom-serializer "0"
1761+
domelementtype "1"
1762+
17091763
ee-first@1.1.1:
17101764
version "1.1.1"
17111765
resolved "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -1730,6 +1784,11 @@ end-of-stream@^1.1.0:
17301784
dependencies:
17311785
once "^1.4.0"
17321786

1787+
entities@^2.0.0:
1788+
version "2.0.3"
1789+
resolved "https://registry.yarnpkg.com/entities/-/entities-2.0.3.tgz#5c487e5742ab93c15abb5da22759b8590ec03b7f"
1790+
integrity sha512-MyoZ0jgnLvB2X3Lg5HqpFmn1kybDiIfEQmKzTb5apr51Rb+T3KdmMiqa70T+bhGnyv7bQ6WMj2QMHpGMmlrUYQ==
1791+
17331792
envinfo@^7.1.0:
17341793
version "7.3.1"
17351794
resolved "https://registry.npmjs.org/envinfo/-/envinfo-7.3.1.tgz#892e42f7bf858b3446d9414ad240dbaf8da52f09"
@@ -2836,6 +2895,11 @@ map-visit@^1.0.0:
28362895
dependencies:
28372896
object-visit "^1.0.0"
28382897

2898+
mdn-data@2.0.6:
2899+
version "2.0.6"
2900+
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.6.tgz#852dc60fcaa5daa2e8cf6c9189c440ed3e042978"
2901+
integrity sha512-rQvjv71olwNHgiTbfPZFkJtjNMciWgswYeciZhtvWLO8bmX3TnhyA62I6sTWOyZssWHJJjY6/KiWwqQsWWsqOA==
2902+
28392903
mem@^1.1.0:
28402904
version "1.1.0"
28412905
resolved "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz#5edd52b485ca1d900fe64895505399a0dfa45f76"
@@ -3395,6 +3459,13 @@ npmlog@^4.0.2:
33953459
gauge "~2.7.3"
33963460
set-blocking "~2.0.0"
33973461

3462+
nth-check@^1.0.2:
3463+
version "1.0.2"
3464+
resolved "https://registry.yarnpkg.com/nth-check/-/nth-check-1.0.2.tgz#b2bd295c37e3dd58a3bf0700376663ba4d9cf05c"
3465+
integrity sha512-WeBOdju8SnzPN5vTUJYxYUxLeXpCaVP5i5e0LF8fg7WORF2Wd7wFX/pk0tYZk7s8T+J7VLy0Da6J1+wCT0AtHg==
3466+
dependencies:
3467+
boolbase "~1.0.0"
3468+
33983469
nullthrows@^1.1.0:
33993470
version "1.1.1"
34003471
resolved "https://registry.npmjs.org/nullthrows/-/nullthrows-1.1.1.tgz#7818258843856ae971eae4208ad7d7eb19a431b1"
@@ -3798,10 +3869,13 @@ react-is@^16.8.1, react-is@^16.8.4:
37983869
resolved "https://registry.npmjs.org/react-is/-/react-is-16.9.0.tgz#21ca9561399aad0ff1a7701c01683e8ca981edcb"
37993870
integrity sha512-tJBzzzIgnnRfEm046qRcURvwQnZVXmuCbscxUO5RWrGTXpon2d4c8mI0D8WE6ydVIm29JiLB6+RslkIvym9Rjw==
38003871

3801-
react-native-svg@^9.7.0:
3802-
version "9.7.0"
3803-
resolved "https://registry.npmjs.org/react-native-svg/-/react-native-svg-9.7.0.tgz#2d0bf1d31d5f1dd061587215b50deb29e626df18"
3804-
integrity sha512-8E1snfe2YYbfu6SP5DOoQgRhCdv7D0F4VewUAV+IgAn+IScrA/uuLB8LCodRdO+9U4Rm5dJ4yIwsVhPHRtuBkw==
3872+
react-native-svg@^9.7.1:
3873+
version "9.13.6"
3874+
resolved "https://registry.yarnpkg.com/react-native-svg/-/react-native-svg-9.13.6.tgz#5365fba2bc460054b90851e71f2a71006a5d373f"
3875+
integrity sha512-vjjuJhEhQCwWjqsgWyGy6/C/LIBM2REDxB40FU1PMhi8T3zQUwUHnA6M15pJKlQG8vaZyA+QnLyIVhjtujRgig==
3876+
dependencies:
3877+
css-select "^2.0.2"
3878+
css-tree "^1.0.0-alpha.37"
38053879

38063880
react-native@^0.60.5:
38073881
version "0.60.5"
@@ -4289,7 +4363,7 @@ source-map@^0.5.0, source-map@^0.5.6:
42894363
resolved "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"
42904364
integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=
42914365

4292-
source-map@^0.6.0, source-map@~0.6.1:
4366+
source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
42934367
version "0.6.1"
42944368
resolved "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
42954369
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

0 commit comments

Comments
 (0)