简体   繁体   English

Android react-native 应用程序没有选择字体

[英]Android react-native app not picking up font

I am trying to use a font "Verveine Corp Regular' inside my react-native app.我正在尝试在我的本机应用程序中使用字体“Verveine Corp Regular”。

The font works in the iOS build, but not in the Android build.该字体适用于 iOS 版本,但不适用于 Android 版本。

The font is in .tff format and is placed in the root of my work (linked in the package.json and I have run react-native link) and inside "android/gradle/src/main/assets/fonts" but it's still not picking the font up.字体为 .tff 格式,并放置在我工作的根目录中(在 package.json 中链接并且我运行了 react-native 链接)和“android/gradle/src/main/assets/fonts”内,但它仍然是不拿起字体。 I have also cleaned and rebuilt the app multiple times.我也多次清理和重建应用程序。

When inspecting an element which uses the font in the android debugger, it says it's using the font.在 android 调试器中检查使用该字体的元素时,它说它正在使用该字体。 But the font is still the default font.但是字体仍然是默认字体。

Could anyone offer some help or guidance on this issue?任何人都可以就这个问题提供一些帮助或指导吗?

Thanks!谢谢!

The other answers helped me get this working for me, thank you.其他答案帮助我让这对我有用,谢谢。 This manuever is probably only necessary when the font has capital letters in the filename.可能仅当字体文件名中包含大写字母时才需要此操作。 A more complete answer:一个更完整的答案:

Add the font as normal in react native, for example:在 react native 中正常添加字体,例如:

{react-native-project}/fonts/GovtAgentBB.ttf {react-native-project}/fonts/GovtAgentBB.ttf

Run react-native link and this will put the font in运行 react-native link 这会将字体放入

{react-native-project}/android/app/src/main/assets/fonts/GovtAgentBB.ttf {react-native-project}/android/app/src/main/assets/fonts/GovtAgentBB.ttf

{react-native-project}/android/app/src/main/assets/fonts/GovtAgentBB_ital.ttf {react-native-project}/android/app/src/main/assets/fonts/GovtAgentBB_ital.ttf

But android, bring robotic and not human, doesn't like this.但是android,带来机器人而不是人类,不喜欢这个。 So rename the file with all lower case letters and an underscore before the variant, like:因此,在变体之前使用所有小写字母和下划线重命名文件,例如:

{react-native-project}/android/app/src/main/assets/fonts/govtagentbb.ttf {react-native-project}/android/app/src/main/assets/fonts/govtagentbb.ttf

{react-native-project}/android/app/src/main/assets/fonts/govtagentbb_ital.ttf {react-native-project}/android/app/src/main/assets/fonts/govtagentbb_ital.ttf

Then, you have to change the font name in the style depending on the platform.然后,您必须根据平台更改样式中的字体名称。 For iOS, use the human name that is the name of the font that would be displaying in the title of the window of the Mac Font menu (or just the name you see on the web).对于 iOS,使用人名,即 Mac 字体菜单窗口标题中显示的字体名称(或只是您在网络上看到的名称)。 For android, you have to, robotically, use the name of the file you just renamed.对于 android,您必须自动使用刚刚重命名的文件的名称。

      {Platform.OS === 'ios' ? (
        <Text style={styles.welcome}>
          Hello World!
        </Text>
      ) : (
        <Text style={styles.welcomeAndroid}>
          Hello World
        </Text>
      )}
      const styles = StyleSheet.create({
      ...
      welcome: {
          fontFamily: 'Government Agent BB',
      },
      welcomeAndroid: {
          fontFamily: 'govtagentbb',
      },

This is how I used custom font in my project这就是我在项目中使用自定义字体的方式

//sampleStyle.js
import overrideStyles from '/sampleoverridestyles';
iconTextStyle: {
  color: '#FFFFFF',
  fontSize: 16
}

//sampleoverridestyles.ios.js
export default {
  iconTextStyle: {
    fontFamily: 'FaktSoftPro-Medium'
  }
}

//sampleoverridestyles.android.js
export default {
  iconTextStyle: {
    fontFamily: 'faktsoftpro_medium'
  }
}

since I cannot set the font name same for iOS and android I have overridden it as above and it worked.因为我无法为 iOS 和 android 设置相同的字体名称,所以我已经按照上面的方式覆盖了它并且它起作用了。

Guys if you are following all other solution and still no update then try following the steps伙计们,如果您正在遵循所有其他解决方案但仍然没有更新,请尝试按照以下步骤操作

  1. add react-native-config.js file添加 react-native-config.js 文件
  2. then add fonts in assets/fonts/your font然后在资产/字体/您的字体中添加字体
  3. rename it with lowercase like OpenSans_Regular to opensans_regular将其重命名为小写,如 OpenSans_Regular 为 opensans_regular
  4. react-native link in terminal in your project folder项目文件夹中终端中的 react-native 链接
  5. after all build your project again using react-native run-android毕竟使用 react-native run-android 再次构建您的项目
  6. fontFamily: Platform.OS === "ios" ? fontFamily: Platform.OS === "ios" ? 'opensans_regular' : 'cassandrapersonaluseregular_3bjg', 'opensans_regular':'cassandrapersonaluserregular_3bjg',

I was also getting the same issue the font was not reflecting changes then I build my project again that was my mistake because we adding fonts in our android folder so we need to compile that again.我也遇到了同样的问题,字体没有反映更改,然后我再次构建我的项目,这是我的错误,因为我们在我们的 android 文件夹中添加了字体,所以我们需要再次编译它。 I hope someone may save their time我希望有人可以节省他们的时间

I added font in react-native android from here:我从这里在 react-native android 中添加了字体:

https://medium.com/@gattermeier/custom-fonts-in-react-native-for-android-b8a331a7d2a7#.40vw3ooar https://medium.com/@gattermeier/custom-fonts-in-react-native-for-android-b8a331a7d2a7#.40vw3ooar

Follow all the steps it will work fine.按照所有步骤它会正常工作。

After adding run react-native run-android添加 run react-native run-android

First, make sure you are up to version 0.16+ with react-native.
Your fonts should be *.ttf or *.otf files and must be located in: /projectname/android/app/src/main/assets/fonts
Make sure the fonts are lowercase only and follow this pattern: fontname.ttf, fontname_bold.ttf, fontname_light.ttf, fontname_bold_italic.ttf

Have you defined another font in your AppTheme (styles.xml), that overrides your preferred font?您是否在 AppTheme (styles.xml) 中定义了另一种字体来覆盖您的首选字体?

Have you tested your font with a "Hello World"-App as a minimal test?您是否使用“Hello World”-App 作为最小测试来测试您的字体?

Have you implemented your ttf as shown here for example?: How to use custom font in Android Studio例如,您是否按照此处所示实现了 ttf?: 如何在 Android Studio 中使用自定义字体

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

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