I have added 4 custom fonts (including supported styles, e.g. bold, black, medium, etc) to my React Native project following the guide:
https://blog.bam.tech/developper-news/add-a-custom-font-to-your-react-native-app
The fonts in question are:
Guardian Egyp
Guardian Sans
Guardian Sans Cond
Guardian Sans XCond
In iOS when I use the fontFamily style property on a <Text> component the following Space Cased family names work:
fontFamily: 'Guardian Egyp'
fontFamily: 'Guardian Sans'
Specifically the correct Regular style for that font is used.
However when I do the same for the following:
fontFamily: 'Guardian Sans Cond'
fontFamily: 'Guardian Sans XCond'
The wrong style of the font is used (it uses bold italic)
However if I use PascalCasing including the style:
fontFamily: 'GuardianSansCond-Regular'
fontFamily: 'GuardianSansXCond-Regular'
It works as expected.
Does anyone have any idea why in the case of the Cond and XCond font families the 'Space Cased' naming for the fontFamily does not work?
All the font families were imported into the RN project in the same way with the same naming conventions.
NOTE: using RN 0.53.0
I had the same error on iOS and couldn't find the reason. It was a wrong name of the font! Check what are the exact names of your fonts. Add the font family and type separately.
You can list your font names using this code in your AppDelegate.m file. Then check in the debugger:
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
Related
I have a set of local fonts that I'd like to apply on my text in PangoML.
AFAIK the way to specify fonts is through the font family attribute in the span element. But where/how can I specify a path to the otf file containing the fonts I'd like to use?
I've also tried setting the font through imagemagick, but then it seems to apply the font to multiple texts.
graphicsMagick(900, 150, "#FCF6EA")
.font(`${__dirname}/static/fonts/Graphik-SemiBold.otf`, 1)
.fontSize(52)
.out('-background', '#FCF6EA')
.out('-size', '890x', 'pango:TOPTEXT_WITH_FIRST_FONT'
.out('-gravity', 'NorthWest', '-geometry', '+10+10')
.out('-compose', 'atop')
.out('-size', '850x', 'pango:BOTTOMTEXT_WITH_DIFFERENT_FONT')
.out('-gravity', 'SouthWest', '-geometry', '+30+10')
.out('-compose', 'atop')
.out('-composite')
I using custom font for my Flutter app:
return MaterialApp (
theme: ThemeData(
fontFamily: "MyFont"
)
)
but in one place of application I need to use "system font". this is especially important for iOS (Sf Pro). If I understand correctly, this is the system font for iOS. But how can I use "system font" if I already use "custom font" at application level?
I tried:
final fontFamily = DefaultTextStyle.of(context).style!.fontFamily;
But this code return "MyFont".
also I tried importing from apple site. but there is only one big SF-Pro.ttf(8Mb) file inside the image. Not - SFPro-Regular.ttf, SFPro-SemiBold.ttf ... Flutter does not support .otf files.
How to solve this problem? any advice I will be grateful
I wanted to use a custom font in my react-native project, but I got an error on ios simulator
How can I solve it
I tried every way but I couldn't find a solution.
Unrecognized Font Family: sfproregular
Error: Error Screenshot
Project Structure:
Project structure screenshot
react-native.config.js
module.exports = {
project: {
ios: {},
android: {}, // grouped into "project"
},
assets: ["./assets/fonts/"], // stays the same
};
versions
"react": "16.13.1",
"react-native": "0.63.2",
"react-native-gesture-handler": "^1.7.0",
"react-native-phone-input": "^0.2.4",
"react-navigation": "^4.4.0"
I wanted to use the font in "Welcome.js"
Welcome.js
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
Image,
StatusBar,
TouchableOpacity,
Button,
ImageBackground,
} from 'react-native';
import Logo from '../components/WelcomeLogo';
import arkaplan from '../assets/images/arkaplan.png';
export default class Welcome extends React.Component {
render() {
return (
<>
<ImageBackground source={arkaplan} style={styles.constrain} >
<StatusBar barStyle="light-content"/>
<Logo />
<Text style={styles.welcome} >Welcome!</Text>
</View>
</>
)
}
};
const styles = StyleSheet.create({
constrain: {
flex: 1,
alignItems:'center',
justifyContent:'center',
welcome: {
fontSize: 50,
color: 'white',
fontFamily: 'sfprogregular',
marginTop: 20,
marginBottom: 5,
},
});
Much like you I experienced this Unrecognized Font family issue when running the iOS build via Terminal. For whatever reason, it doesn't effect the final archive build or if one runs the build via Xcode. This is on the latest RN 0.63.3 in macOS Catalina 10.15.7 with Xcode 12.0.1.
The issue was with the font name itself.
Solution Summary
In your case, I recommend the following steps to take.
Install your font sfprogregular on your macOS system
With the font installed, and selected in Font Book press CMD+I to see postscript name
Rename the font files in your project to their respective PostScript names
Run npx react-native link in your project to setup the renamed fonts
Do cleanup of the older font files prior to the rename (remove old fonts)
Change the name of the font to the postscript name in code when referring to the font in styles
My experience detailed
In my case, we are using TradeGothic.
Initially, when we linked the font, the name of the font file itself was:
Trade Gothic LT.ttf
Android is fine with this, but not iOS. That's because Android relies on the filename, but iOS depends on the PostScript name of the font.
To fix this, I renamed the ttf file to it's postscript name TradeGothicLT.ttf. For the bold one which was Trade Gothic LT Bold.ttf I renamed it to TradeGothicLT-Bold.ttf.
I then did npx react-native link inside my project to connect my newly renamed fonts. Then, I cleaned up by removing the older .ttf files from the XCode project under resources (just press delete on each of the red font files that are no longer there) and removed the font files from the older Android link process in the folder /android/app/src/main/assets/fonts/. In the info.plist file, under the section UIAppFonts remove the older font filenames.
Finally in code, instead of referring to 'Trade Gothic LT' we now refer to it as 'TradeGothicLT'. That's it, it works now!
You can find the postscript name of the font by installing the .ttf file (double click it) on macOS and in Font Book with the font selected press CMD+I to get information about the font.
NOTE: It's not necessary to install the font to macOS, it's just the only way I know of to get the PostScript name. If you already have the PostScript name of the fonts, you don't need to install them.
I hope this helps you and others that might stumble upon this issue!
After adding the custom fonts, you need to link it using react-native link.
That will create an entry in Info.plist file (iOS) & android/src/main/assets/fonts/ directory (Android).
If the above command fails, you need to add those fonts manually in the android directory & plist file.
Sometimes your font name is not the same for Android and iOS (since iOS uses postscript name as fontFamily). In most cases, in android, the font is the same as the file, for example, I had this problem when using HelveticaNeueLTPro-BdEx font in iOS.
I am sharing the fix that helps me out.
Run npx react-native link. Maybe you already did.
Add this inside the didFinishLaunchingWithOptions method on your AppDelegate.m:
for (NSString *familyName in [UIFont familyNames]){
NSLog(#"Family name: %#", familyName);
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(#"--Font name: %#", fontName);
}
}
Then, when you launch your app with xcode, you will be able to see all the fonts installed on your app, so you just have to use it the way it is listed on your console:
You can add fontFamily based on OS like:
Platform.OS == 'android' ? 'Linotype - HelveticaNeueLTPro-BlkEx' : 'HelveticaNeueLTPro-BlkEx'
Or you can rename the font file name and then run npx react-native link again and also maybe have to delete the previous UIAppFonts entry for the previously linked font in Info.plist file.
I've created an React Native app, targeted mainly at iOS.
I'm using custom fonts, and can see they are successfully integrated into the app via some debug in AppDelegate.m (in XCode):
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
resulting in:-
MyFontFamily
MyFontFamilyFontOne
MyFontFamilyFontTwo
My issue is accessing the specific fonts in a < Text > element - I always see the same font rendered when using the family or the exact font name, e.g.
<Text style={{fontFamily: 'MyFontFamily'}}>hello</Text>
<Text style={{fontFamily: 'MyFontFamilyFontOne'}}>hello</Text>
<Text style={{fontFamily: 'MyFontFamilyFontTwo'}}>hello</Text>
results in the same font rendered.
Outputting the same in HTML results in the desired outcome, e.g. fonts One and Two are different.
As said by #Yanush verified you have properly linked your fonts. After with custom font you must set the fontFamily, the fontWeight and the fontStyle
Make sure you have linked the fonts correctly.
The correct way of linking fonts in react native is as described:
Create a fonts dir under your project dir, eg: ./assets/fonts
Edit package.json file and add these lines:
"rnpm": {
"assets": [
"./app/assets/fonts"
]
}
Link the fonts: in your project dir run "react-native link"
I'm using a custom font (BebasNeueLight). It looks like this in Font Book:
I've added it to the iOS project in the usual style; copied it into the project added a key in the plist "Fonts provided by application" and added the name of the file (BebasNeueLight.otf).
In my label in Swift I do the following:
label.font = UIFont(name: "BebasNeueLight", size: 24.0)!
Which is the actual Postscript name of the font.
When I run the app I see the custom font is loaded; but it displays the regular style (which I neither added to my project or specified):
Font book screenshot with the 'regular' style:
Has anyone seen this before? I'm guessing there is something 'wrong' with the font itself. I works fine on Android btw.
-- Edit:
I'm using the correct name, I've gotten this name with fontconfig and Swift code for printing font (and it is loading the custom font; just not the correct style..). Output:
❯ fc-scan --format "%{postscriptname}\n" BebasNeueLight.otf
BebasNeueLight
--
The Storyboard also renders the font 'wrong'; I've selected the font with my label (as attributed so you can see the render preview):
But the Storyboard already renders it as the 'regular' type (just as the app):
So I'm thinking it is a problem with the font itself for some reason.
I guess the problem is at font name "BebasNeueLight", put below function at your app delegate and call it:
func printFonts() {
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
}
then check the name of loaded font family name may be "BebasNeue-Light"
The problem is that "BebasNeueLight" in font file name does not correspond with real font title name. So you need to find you the font name somehow. One of the variants is:
func listFontsNames(){
for family : String in UIFont.familyNames as [String]
{
print("Family : \(family)")
for name in UIFont.fontNames(forFamilyName: family)
{
print("name : \(name)")
}
}
}
Of course you've added font to project and to info.plist?
1.Add font file into project
2.Add a new entry with the key "Fonts provided by application" in plist file
each of your files, add the file name to this array in Fonts provided by application
3.label.font = [UIFont fontWithName:#"BebasNeueLight" size:15];
Swift.
label.font = UIFont(name:"BebasNeueLight", size:15)
you can print all fonts (see below). You can also check if the font exists.
for family in UIFont.familyNames {
print("\(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print(" \(name)") // <- FontName
}
}
copy and paste your font name from log and try to assign:
label.font = UIFont(name: "<PasteYourFontNameHere>", size: 24.0)!
sometimes the font name is different.
Did you setup everything from xcode etc? If not please follow this
If you are using webfont then download.ttf file and drop it into your project . Check mark on copy items if needed
Next add this on info plist
<key>UIAppFonts</key>
<array>
<string>Your fontname.ttf</string>
<string>Bakersfield Bold.ttf</string>
</array>
Now take a look the font family name. Which you will find on font file also. From where you have downloaded you will get there also. Like i added font which ttf file name is : Bakersfield Bold.ttf for this fontname is : Bakersfield-Bold Thats it Happy coding.
if you already done then follow this steps
label.font = UIFont(name: "BebasNeue-Light", size: 24.0)!
May be here you made a mistake on font name.
The latest iOS SDK and Xcode (9.3) fixes this problem, so I guess it was a bug in IOS < 11.3.
Current version in which it works as expected:
❯ xcodebuild -showsdks
iOS SDKs:
iOS 11.3 -sdk iphoneos11.3
The device also needs to be running at least iOS 11.3!
All the other setup (adding font to plist.info, using correct postscript font-name etc) was correct.