简体   繁体   English

Font Awesome 在我的本机项目中无法识别

[英]Font Awesome is unrecongized in my react native project

I have react-native elements installed in my app but I keep getting this error that says Unrecognized Font family Awesome.我在我的应用程序中安装了 react-native 元素,但我不断收到这个错误,提示无法识别的字体系列很棒。

I've reset my cache, did npm install, and removed the build folder and re added it.我重置了缓存,安装了 npm,删除了 build 文件夹并重新添加了它。 Nothing seems to work, any ideas?似乎没有任何效果,有什么想法吗?

The app should run with no errors.该应用程序应该没有错误地运行。 Before I used to get a "cfbundleidentifier does not exist" error but when I thought i had fixed it.在我曾经收到“cfbundleidentifier 不存在”错误之前,但当我认为我已经修复了它时。 I got this unrecognized fontfamily error.我得到了这个无法识别的 fontfamily 错误。 在此处输入图片说明

it's most probably because it can't locate the icons, so you'll need to do这很可能是因为它找不到图标,所以你需要做

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

and then link it然后链接它

react-native link react-native-vector-icons

So, after hours of researching, I found the solution.因此,经过数小时的研究,我找到了解决方案。 The probleme with Fontawesome or WHATEVER font we want to add on RN V0.6> is that the fonts from "react-native- vector-icons" are not linked well from the node_modules folder.我们想在 RN V0.6> 上添加 Fontawesome 或 WHATEVER 字体的问题是“react-native-vector-icons”中的字体没有很好地与 node_modules 文件夹链接。 This is what I did:这就是我所做的:

  • Create a folder "assets/fonts" on the RN root project在 RN 根项目上创建文件夹“assets/fonts”

  • Copy the ttf files on the created folder and, HERE IS THE BUG, the name of the files has to be "lowercase.ttf" or "lowercase_whatever_also_lowercase.ttf".复制创建的文件夹中的 ttf 文件,这是错误所在,文件名必须是“lowercase.ttf”或“lowercase_whatever_also_lowercase.ttf”。 If you put Uppercases or "-" it will crash.如果您输入大写字母或“-”,它会崩溃。 don't know why.不知道为什么。

  • Create a file "react-native.config.js" on the RN root project and paste the following:在 RN 根项目上创建一个文件“react-native.config.js”并粘贴以下内容:

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

And finnally execute the follwing on the terminal:最后在终端上执行以下操作:

react-native start --reset-cache
react-native link
npm run ios

I tested for IOS and it solved.我对IOS进行了测试,它解决了。 The exhausting matter was the UPPERCASE question that I could not imagine.令人筋疲力尽的问题是我无法想象的大写问题。

You have to follow some steps to add fonts to your project, check each step.您必须按照一些步骤将字体添加到您的项目中,检查每个步骤。

  1. Add the font folder into your project on the path在路径上将字体文件夹添加到您的项目中

    AppName/android/assets/fonts/ AppName/android/assets/fonts/

    AppName/ios/assets/fonts/应用名称/ios/assets/fonts/

  2. Link your font with project.将您的字体与项目联系起来。 for this,为了这,

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

Add this line to your project's package.json and run,将此行添加到您项目的package.json并运行,

react-native link
  1. App should be rebuilt for the font to be recognized:应重建应用程序以识别字体:

    react-native run-ios react-native run-ios

    react-native run-android react-native run-android

if you don't know the exact name of font, you can add the code in AppDelegate.m inside the function,如果您不知道字体的确切名称,可以在函数内的 AppDelegate.m 中添加代码,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...


  NSArray *fontFamilies = [UIFont familyNames];

  for (int i = 0; i < [fontFamilies count]; i++)
  {
    NSString *fontFamily = [fontFamilies objectAtIndex:i];
    NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
    NSLog (@"%@: %@", fontFamily, fontNames);
  }

}

then check the log for exact name of font.然后检查日志以获取字体的确切名称。

And add the font name to your project,并将字体名称添加到您的项目中,

fontFamily: 'fontName'

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

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