简体   繁体   English

react-native react-native-vector-icons:如何使用字体真棒图标

[英]react-native react-native-vector-icons: How to use font awesome icons

In my react-native application I have added this library react-native-vector-icons to use icons.在我的 react-native 应用程序中,我添加了这个库 react-native-vector-icons 来使用图标。 From the github page, FontAwesome is listed among the bundled icon sets.从 github 页面,FontAwesome 列在捆绑的图标集中。 But I don't see how I can reference a fontawesome item in my element.但是我不知道如何在我的元素中引用一个 fontawesome 项目。 It seems that I can only use "ios-xxx", "md-xxx" or "log-xxx".看来我只能使用“ios-xxx”、“md-xxx”或“log-xxx”。

When I try something like:当我尝试类似的事情时:

<Icon name='fa-rss' style={{marginRight:10}} /> 

I get this warning:我收到此警告:

在此处输入图片说明

Use

import Icon from 'react-native-vector-icons/FontAwesome';

<Icon name="rss" size={30} color="#900" />

Always search for the icon name here: FontAwesome and here: Material-icons to be sure that you are using the correct name.始终在此处搜索图标名称: FontAwesome和此处: Material-icons以确保您使用正确的名称。 Then simply you can use the code as:然后你可以简单地使用代码:

import Icon from 'react-native-vector-icons/FontAwesome';
<Icon name="rocket" size={30} color="#900" />    // Here use the correct
 name from the first link above.

For more info refer this有关更多信息,请参阅

Since there is a very large amount of them, and I don't want to import them all, I ended up importing each icon I need as an SVG.由于它们的数量非常多,而且我不想将它们全部导入,因此我最终将我需要的每个图标作为 SVG 导入。

then paste it to your JSX file然后将其粘贴到您的 JSX 文件中

import * as React from "react";

import { SvgXml, SvgProps } from 'react-native-svg'

const xml = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M470.1 231.3s7.6 37.2 9.3 45H446c3.3-8.9 16-43.5 16-43.5-.2.3 3.3-9.1 5.3-14.9l2.8 13.4zM576 80v352c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V80c0-26.5 21.5-48 48-48h480c26.5 0 48 21.5 48 48zM152.5 331.2L215.7 176h-42.5l-39.3 106-4.3-21.5-14-71.4c-2.3-9.9-9.4-12.7-18.2-13.1H32.7l-.7 3.1c15.8 4 29.9 9.8 42.2 17.1l35.8 135h42.5zm94.4.2L272.1 176h-40.2l-25.1 155.4h40.1zm139.9-50.8c.2-17.7-10.6-31.2-33.7-42.3-14.1-7.1-22.7-11.9-22.7-19.2.2-6.6 7.3-13.4 23.1-13.4 13.1-.3 22.7 2.8 29.9 5.9l3.6 1.7 5.5-33.6c-7.9-3.1-20.5-6.6-36-6.6-39.7 0-67.6 21.2-67.8 51.4-.3 22.3 20 34.7 35.2 42.2 15.5 7.6 20.8 12.6 20.8 19.3-.2 10.4-12.6 15.2-24.1 15.2-16 0-24.6-2.5-37.7-8.3l-5.3-2.5-5.6 34.9c9.4 4.3 26.8 8.1 44.8 8.3 42.2.1 69.7-20.8 70-53zM528 331.4L495.6 176h-31.1c-9.6 0-16.9 2.8-21 12.9l-59.7 142.5H426s6.9-19.2 8.4-23.3H486c1.2 5.5 4.8 23.3 4.8 23.3H528z"/></svg>`

export const VisaIcon = (props: SvgProps) => <SvgXml xml={xml} {...props} />

Don't forget to pass width , height and fill properties to your icon.不要忘记将widthheightfill属性传递给您的图标。

Looking in fontAwesome , find your icon and click and details inside the class ie:查看fontAwesome ,找到您的图标并单击类中的详细信息,即:

<i class="fas fa-home"></i>

have the prop fa-home use faHome in you code without '-'.让道具fa-home在你的代码中使用faHome而不带“-”。

You can try to typing and the VSCode suggest the name of icon like image below:您可以尝试输入,VSCode 会建议如下图所示的图标名称:

例子1

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

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