简体   繁体   English

React-native-vector-icons + Icomoon 的自定义图标集问题

[英]Custom Icon Set Problem with React-native-vector-icons + Icomoon

I want to use my custom icons with react-native-vector-icons and Icomoon.我想将我的自定义图标与 react-native-vector-icons 和 Icomoon 一起使用。 I generated.tff and selection.js with Icomoon and put them into my project with react-native-link but The icons that I tried to use seen as empty square.我使用 Icomoon 生成了.tff 和 selection.js 并使用 react-native-link 将它们放入我的项目中,但是我尝试使用的图标被视为空方块。

I will share with you my code and screenshoot.我将与您分享我的代码和屏幕截图。

Here is my import code这是我的导入代码

import { createIconSetFromIcoMoon } from 'react-native-vector-icons';
import selectionConfig from "../selection.json"
const Icon = createIconSetFromIcoMoon(selectionConfig,"icomoon","icomon.ttf");
<Icon name="bag" size={64} />

This is my package.json edit:这是我的 package.json 编辑:

 "rnpm": {
    "assets": [
        "resources/fonts"
    ]
},

And this is the screenshot of icon: Screenshot这是图标的截图:截图

Note: I put my font files under "./resources/fonts" and put selection.json under my "src" folder and I used "react-native link react-native-vector-icons" code for link these"注意:我将我的字体文件放在“./resources/fonts”下,并将 selection.json 放在我的“src”文件夹下,我使用“react-native link react-native-vector-icons”代码链接这些“

How Can I solve this issue?我该如何解决这个问题?

I think maybe you have forgetten to run react native link, if you did and still does not show the icon, try delete the build file and retry.我想也许您忘记了运行 react native link,如果您这样做了但仍然没有显示图标,请尝试删除构建文件并重试。

There are two way to get your custom icon working, if linking does not help you, you may try the manual way.有两种方法可以让您的自定义图标工作,如果链接对您没有帮助,您可以尝试手动方式。

This answer is referenced here: https://medium.com/bam-tech/add-custom-icons-to-your-react-native-application-f039c244386c这里引用了这个答案: https : //medium.com/bam-tech/add-custom-icons-to-your-react-native-application-f039c244386c

a) React Native link a) 反应本机链接

  1. Put your .ttf in a ./resources/fonts folder at the base of your project将您的 .ttf 放在项目底部的 ./resources/fonts 文件夹中

  2. Add this piece of code at the first level of your package.json :在 package.json 的第一级添加这段代码:

    "rnpm": { "assets": [ "resources/fonts" ] }, "rnpm": { "assets": [ "资源/字体" ] },

  3. In your terminal: react-native link在您的终端中:react-native 链接

b) Manual b) 手册

Android: Copy your .ttf inside the ./android/app/src/main/assets/fonts folder of your RN project. Android:将 .ttf 复制到 RN 项目的./android/app/src/main/assets/fonts文件夹中。

If it still does not show如果还是不显示

delete android build folder and rerun删除android build文件夹并重新运行

I had similar issues and it's because the rnpm in the package is depreciated , I solved it by creating react-native.config.js file and then I did run the link command:我遇到了类似的问题,这是因为包中的rnpm折旧,我通过创建react-native.config.js文件解决了它,然后我确实运行了链接命令:

Here is react-native.config.js file.这是react-native.config.js文件。

module.exports = {
  project: {
    ios: {},
    android: {},
  },
  assets: ['./resources/fonts'],
};

After creating a file and adding the above code snippet, run the following command:创建文件并添加上述代码片段后,运行以下命令:

react-native link

If you prefer to use icons as SVG, there is an easier way to do this.如果您更喜欢将图标用作 SVG,有一种更简单的方法可以做到这一点。 I hope react-icomoon will be of use to you.我希望react-icomoon对你有用。

Create Icon component创建图标组件

import IcoMoon from "react-icomoon";
import { Svg, Path } from "react-native-svg";
const iconSet = require("./selection.json");

const Icon = (props) => (
  <IcoMoon
    native
    SvgComponent={Svg}
    PathComponent={Path}
    iconSet={iconSet}
    {...props}
  />
);

export default Icon;

Then use然后使用

import Icon from "./Icon";

<Icon icon="pencil" size={20} color="orange" />

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

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