react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Can't Set fonts

Open aakashsigdel opened this issue 10 years ago • 7 comments

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

aakashsigdel avatar Feb 20 '16 11:02 aakashsigdel

Have you tried to use a fontWeight?

Kureev avatar Feb 20 '16 14:02 Kureev

@Kureev Yes I have, with the same result.

aakashsigdel avatar Feb 20 '16 14:02 aakashsigdel

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

Kureev avatar Feb 20 '16 18:02 Kureev

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.

grabbou avatar Feb 26 '16 15:02 grabbou

@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.

cesardeazevedo avatar Mar 02 '16 03:03 cesardeazevedo

@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.

aakashsigdel avatar Mar 02 '16 03:03 aakashsigdel

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.

rainer-liao avatar Apr 27 '16 06:04 rainer-liao