Skip to content

Can't Set fonts #6046

Closed
Closed
@aakashsigdel

Description

@aakashsigdel

I am trying to use costum fonts.
I got the idea from this blog post and it was working fine
https://medium.com/@dabit3/adding-custom-fonts-to-react-native-b266b41bff7f#.13fe3uz08

But now I added the font called Lantinghei.ttc (that is available on El Captain by default) to my application, listed it on Info.plist as well.

When I print all the font available to my application to the console using the code mentioned in the above blog:

for (NSString* family in [UIFont familyNames])
{
  NSLog(@”%@”, family);
  for (NSString* name in [UIFont fontNamesForFamilyName: family])
  {
    NSLog(@” %@”, name);
  }
}

it gives me output as

Lantinghei TC
    FZLTTHB--B51-0
    FZLTZHB--B51-0
    FZLTXHB--B51-0
Lantinghei SC
    FZLTZHK--GBK1-0
    FZLTTHK--GBK1-0
    FZLTXHK--GBK1-0

Using fontFamily: Lantinghei TC gives me Lantinghei TC Heavy and using fontFamily: Lantinghei SC gives me Lantinghei SC Heavy.
There is no way for me to set the ExtraLight and DemiBold option of the font. When I open the font in FontBook, it shows me all three options of Heavy, DemiBold and ExtraLight

If I use the other 3 weird name listed above, it gives me the same effect no matter which of the 3 I use.

I created a swift project and used those 3 wired name, and it seems to render fine. But in RN, it has no effect than to render the Heavy font.

Here the minimal code I used to reproduce the issue:

'use strict';
import React, {
  AppRegistry,
  Component,
  StyleSheet,
  Text,
  View
} from 'react-native';

class FontTest extends Component {
  render() {
    return (
      <Text style={styles.welcome}>
        Welcome to React Native!
      </Text>
    );
  }
}

const styles = StyleSheet.create({
  welcome: {
    fontFamily: 'FZLTXHK--GBK1-0',
    fontSize: 20,
    textAlign: 'center',
    margin: 40,
  },
});

AppRegistry.registerComponent('FontTest', () => FontTest);
  • react-native version: 0.20 (but the problem persisted in earlier versions too)
  • I tested it on IOS, i did not know how to print the name of custom fonts that I can use on android
  • OS: Mac OSX El Capitan 10.11.2

Activity

Kureev

Kureev commented on Feb 20, 2016

@Kureev
Contributor

Have you tried to use a fontWeight?

aakashsigdel

aakashsigdel commented on Feb 20, 2016

@aakashsigdel
Author

@Kureev Yes I have, with the same result.

Kureev

Kureev commented on Feb 20, 2016

@Kureev
Contributor

Well, in this case I don't know what to advice. Maybe you can try to convert .ttc to .ttf and try again, but it's just a guess

grabbou

grabbou commented on Feb 26, 2016

@grabbou
Contributor

I've been always explicit with the font names, that is, I was setting the font name to be Lato Bold instead of just Lato & fontWeight. That's been working well.

cesardeazevedo

cesardeazevedo commented on Mar 2, 2016

@cesardeazevedo

@grabbou You just nailed it, i've been struggling with a specifically font in these last days, in my case was Axis font, and it didn't work to just rename the font file, what i did was open the font on FontForge and rename from there to Axis-Font and export to .ttf, i still don't know why, but it worked, thanks.

aakashsigdel

aakashsigdel commented on Mar 2, 2016

@aakashsigdel
Author

@Kureev I opened the font in a font converting app. I think the issue was with some kind of font post script.

The post script had those weird names
FZLTTHB--B51-0
FZLTZHB--B51-0
FZLTXHB--B51-0

The app changed those name to the sane font names. And everything worked fine.

rainer-liao

rainer-liao commented on Apr 27, 2016

@rainer-liao

Can't use custom font in React Native iOS.
My font family name is "Impact".
In RCTConvert.m

+ (UIFont *)UIFont:(UIFont *)font withFamily:(id)family
              size:(id)size weight:(id)weight style:(id)style
   scaleMultiplier:(CGFloat)scaleMultiplier
  UIFont *bestMatch = font;
  CGFloat closestWeight = INFINITY;
  for (NSString *name in [UIFont fontNamesForFamilyName:familyName]) {
    UIFont *match = [UIFont fontWithName:name size:fontSize];
    if (isItalic == RCTFontIsItalic(match) &&
        isCondensed == RCTFontIsCondensed(match)) {
      CGFloat testWeight = RCTWeightOfFont(match);
      if (ABS(testWeight - fontWeight) < ABS(closestWeight - fontWeight)) {
        bestMatch = match;
        closestWeight = testWeight;
      }
    }
  }
return bestMatch;

I find when I use font family "Impact", the code

isItalic == RCTFontIsItalic(match) &&
        isCondensed == RCTFontIsCondensed(match)

return false so bestMatch return nil.

I also find other custom font families work fine. Just "Impact" don't.

locked as resolved and limited conversation to collaborators on May 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Kureev@grabbou@aakashsigdel@rainer-liao@cesardeazevedo

        Issue actions

          Can't Set fonts · Issue #6046 · facebook/react-native