简体   繁体   English

如何将自定义图标添加到 NativeBase

[英]How to add custom Icons to NativeBase

I have the .ttf and .svg files for customized icons that I designed.我有我设计的自定义图标的.ttf.svg文件。 But I would like to import them into my NativeBase project, making them available for use in NB's <Icon /> component.但我想将它们导入到我的 NativeBase 项目中,使它们可用于 NB 的<Icon />组件。 How can I achieve this?我怎样才能做到这一点?

React Native Custom Icon Font for NativeBase用于 NativeBase 的 React Native 自定义图标字体

Adopted from: https://medium.com/@kelleyannerose/react-native-custom-icon-font-with-no-sad-red-screen-72b8d09a0e7b摘自: https : //medium.com/@kelleyannerose/react-native-custom-icon-font-with-no-sad-red-screen-72b8d09a0e7b

This is what worked for me :)这对我有用:)


Step 0: React Native Vector Icons install and link Make sure you have react-native-vector-icons installed and linked.第 0 步: React Native Vector Icons 安装和链接 确保你已经安装并链接了 react-native-vector-icons。 If you haven't installed it yet, follow install instructions from their docs.如果您还没有安装它,请按照他们的文档中的安装说明进行操作。 Usually these two commands should do it for you.通常这两个命令应该为你做。

npm install react-native-vector-icons --save
react-native link

Step 1: Add icons in Fontello First, you have to get your icons into a font.第 1 步:在 Fontello 中添加图标 首先,您必须将图标转换为字体。 Go to fontello.com , and as their UI so accurately suggests, 'Drag custom SVG icons or SVG font here'.转到fontello.com ,正如他们的 UI 如此准确地建议的那样,“将自定义 SVG 图标或 SVG 字体拖到此处”。

在此处输入图片说明


Step 2: Select icons This might not be as obvious, but after you upload the icons, you have to select which you want to include (one by one).第 2 步:选择图标 这可能不是很明显,但是在上传图标后,您必须选择要包含的图标(一个一个)。 This is nice because you can choose icons from other libraries if you want to and they'll all be a part of your one icon font.这很好,因为您可以根据需要从其他库中选择图标,并且它们都将成为您的一个图标字体的一部分。 You'll see your selections clearly with a red circle.您将使用红色圆圈清楚地看到您的选择。

在此处输入图片说明


STEP 3 / BIG TIP: NAME YOUR FONT I recommend naming your font in the little box next to the big red button in Fontello.第 3/重要提示:命名您的字体 我建议在 Fontello 中红色大按钮旁边的小框中命名您的字体。 This way there won't be any questions what your font name is.这样就不会有任何问题你的字体名称是什么。 And the file name will match the font name, so you can use the same name in iOS and Android leaving less room for error — phew.并且文件名将与字体名称匹配,因此您可以在 iOS 和 Android 中使用相同的名称,从而减少出错的空间——呸。

在此处输入图片说明


Step 4: Download webfont Click the red 'Download webfont' button in top right.第 4 步:下载 webfont 点击右上角的红色“下载 webfont”按钮。 You can get the config file on its own by clicking the down arrow, but you'll need the .ttf file as well.您可以通过单击向下箭头自行获取配置文件,但您还需要 .ttf 文件。

在此处输入图片说明


Step 5: Add .ttf file to project Find the .ttf file in the fonts directory from your download and add it somewhere in your src file that makes sense.第 5 步:将 .ttf 文件添加到项目从下载的 fonts 目录中找到.ttf文件,并将其添加到src文件中有意义的某个位置。 I usually have a src/assets/fonts directory that I use.我通常有一个我使用的src/assets/fonts目录。 I like to have this file accessible somewhere in a shared file for my own sanity.为了我自己的理智,我喜欢在共享文件的某个地方访问这个文件。


Step 6 (iOS): Add .ttf to Resources in Xcode Open the project in Xcode ios/yourprojectname.xcodeproj .第 6 步(iOS):将 .ttf 添加到 Xcode 中的资源 在 Xcode ios/yourprojectname.xcodeproj打开项目。 Once in Xcode, right click on the resources directory, select 'Add files to “yourprojectname”…' and select the custom icon font .ttf file (I have mine available in that src/assets/fonts directory I mention in step 5).在 Xcode 中,右键单击资源目录,选择“将文件添加到“您的项目名称”...”并选择自定义图标字体 .ttf 文件(我在第 5 步中提到的src/assets/fonts目录中有我的可用)。


Step 7 (iOS): ADD FONT TO COPY BUNDLE RESOURCES While still in Xcode, go to 'Build Phases' in the top bar, open 'Copy Bundle Resources' and you'll see a list of the icon fonts from react-native-vector-icons.第 7 步(iOS):添加字体以复制捆绑资源仍然在 Xcode 中时,转到顶部栏中的“构建阶段”,打开“复制捆绑资源”,您将看到来自 react-native 的图标字体列表-矢量图标。 Check for your icon font name in that list, if it's not there, click the '+' and select your .ttf file.检查该列表中的图标字体名称,如果不存在,请单击“+”并选择您的 .ttf 文件。

在此处输入图片说明


Step 8 (iOS) : ADD TO INFO.PLIST Open your info.plist file (preferably before you add any additional targets, because this is the info.plist that will get copied) and open 'Fonts provided by application.第 8 步(iOS) :添加到 INFO.PLIST 打开您的 info.plist 文件(最好在添加任何其他目标之前,因为这是将被复制的 info.plist)并打开“应用程序提供的字体”。 Click the little circled '+'.单击带圆圈的小“+”。 It'll add a line at the top for you to type in your font file name.它会在顶部添加一行供您输入字体文件名。

在此处输入图片说明


Step 9 (Android): Add .ttf to Android Add the .ttf file to the android/app/src/main/assets/fonts directory.步骤 9 (Android):.ttf添加到 Android 将.ttf文件添加到android/app/src/main/assets/fonts目录。 This should already exist because you've already installed react-native-vector-icons and run react-native link which places all it's icon font files here for Android.这应该已经存在,因为您已经安装了react-native-vector-icons并运行react-native link将它的所有图标字体文件放在此处以供 Android 使用。


Step 10: Add configuration and the icon component to your project Back to the shared src directory!第十步:在你的项目中添加配置和图标组件回到共享的src目录! Add two files somewhere in your shared src directory called icon-font.js and icon-font.json or whatever you want to call them.在共享src目录中的某个位置添加两个文件,名为icon-font.jsicon-font.json或任何你想调用它们的文件。 I like to have a src/config directory for (you guessed it) configuration stuff.我喜欢有一个src/config目录用于(你猜对了)配置内容。

Go back to the files you downloaded from Fontello, grab the config.json contents and paste them into your icon-font.json file.返回您从 Fontello 下载的文件,获取config.json内容并将它们粘贴到您的icon-font.json文件中。

Open your icon-font.js file and paste the following (replacing kelleyicons with whatever you named your custom icon font of course).打开您的icon-font.js文件并粘贴以下内容( kelleyicons用您命名的自定义图标字体替换kelleyicons )。

import { createIconSetFromFontello } from 'react-native-vector-icons';
import fontelloConfig from './icon-font.json';
const MyIcon = createIconSetFromFontello(fontelloConfig, 'kelleyicons');
export default MyIcon;

Step 11: Add instance Before running the app, go ahead and add an instance of your icon font somewhere so you can double check it's working right off the bat.第 11 步:添加实例 在运行应用程序之前,继续并在某处添加图标字体的实例,以便您可以立即检查它是否正常工作。 In any of your component or screen files, import your icon from the icon-font.js file.在您的任何组件或屏幕文件中,从icon-font.js文件中导入您的图标。

import MyIcon from './../config/icon-font.js';

And in your view somewhere, use it like so (becasue I'm using NativeBase I wrapped the custom Icon in NativeBase's Icon component).在您看来,像这样使用它(因为我使用的是 NativeBase,我将自定义 Icon 包装在 NativeBase 的Icon组件中)。 You can find the individual names to use in the icon-font.json file.您可以在icon-font.json文件中找到要使用的各个名称。

<Icon><MyIcon
  name={'chrome'}
  size={20}
  color={'#333333'} /></Icon>

Step 12: Save, run and (hopefully) celebrate!第 12 步:保存、运行并(希望)庆祝! Make sure everything is saved up, go to your terminal, run npm start , open your app, and cross your fingers for zero errors!确保所有内容run npm start保存,转到您的终端, run npm start ,打开您的应用程序,然后run npm start零错误!


BONUS TIP!奖金提示!

As of React Native 0.60 resources are automatically linked.从 React Native 0.60 开始,资源会自动链接。 This means that if you run pod install (which you eventually will) you may see an error saying 'Multiple commands produce' error when building with new Xcode build system' .这意味着,如果您运行pod install (您最终会这样做),您可能会看到一条错误消息,说'Multiple commands produce' error when building with new Xcode build system' If you are running the New Build System (File > Workplace Settings > Build System) .如果您正在运行New Build System (File > Workplace Settings > Build System) This error is because React Native is trying to link the same resources twice.这个错误是因为 React Native 试图两次链接相同的资源。 If this happens to you, simply go back to Step 7 (iOS): ADD FONT TO COPY BUNDLE RESOURCES select all the .ttf files and press the - button to remove.如果您遇到这种情况,只需返回步骤 7 (iOS):添加字体以复制捆绑资源选择所有 .ttf 文件并按-按钮删除。 Then build again :)然后再次构建:)

React Native Custom Icon for NativeBase NativeBase 的 React Native 自定义图标

NativeBase provides you with createIcon , which you can use to create and export your own icons, individually, using the viewBox and the path data of your .svg files. NativeBase 为您提供了createIcon ,您可以使用它使用viewBox.svg文件的路径数据单独创建和导出自己的图标。

One path一条路

If your icon only has one path data, you can supply it as such:如果你的图标只有一个路径数据,你可以这样提供:

import React from 'react';
import { createIcon } from 'native-base';

export const MyCustomIcon = createIcon({
  viewBox: '0 0 512 512',
  d: 'M424.337,272.548c-72.91,...,8.532v16.549H271.514z',
});

Multiple paths多路径

Using Path and G from react-native-svg , you can supply the said paths to the path property in createIcon parameters.使用react-native-svg中的PathG ,您可以将上述路径提供给createIcon参数中的路径属性。

Here is an example:这是一个例子:

import React from 'react';
import { createIcon } from 'native-base';
import { Path, G } from 'react-native-svg';

export const MyCustomIcon = createIcon({
  viewBox: '0 0 512 512',
  path: <G>
    <Path d="M486.4,0H25.6C11.468,...,8.533V486.4z" />
    <Path d="M452.267,...,8.533V452.267z" />
  </G>
});

Bonus奖金

You can even use useToken to give your custom icon the colors of your theme您甚至可以使用useToken为您的自定义图标提供主题的 colors

import React from 'react';
import { createIcon, useToken } from 'native-base';
import { Path, G } from 'react-native-svg';

export const MyCustomIcon = (props) => {
  const [white, primary, secondary] = useToken('colors', ['white', 'primary.400', 'secondary.400']);

  const component = createIcon({
    viewBox: '0 0 370 370',
    path: <G>
      <Path fill={primary} d="M350.00159,370.00153h-230a20,...,8.533V452.267z" />
      <Path fill={white} d="M250.00159,40.00154h-230a20,...,40Z" />
      <Path fill={secondary} d="M135.1,219.84,36.41,...,338.57l39.49-56.66Z" />
    </G>,
    ...props
  });

  return component.type.render();
};

We do something like below to show .ttf and .svg icons in React我们做类似下面的事情来在 React 中显示 .ttf 和 .svg 图标

  import ttfFile from “./fonts/fileName.ttf”;
  import svgFile from “./svg/fileName.svg”

  <img src={ttfFile} />
  <img src={svgFile} />

Or或者

 <img src={require(“./fonts/fileName.ttf”)}/>
  <img src={require(“./svg/fileName.svg”)} />

Excuse me for wrong double quotes typo because I am answering from my mobile请原谅我打错了双引号,因为我是用手机回答的

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

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