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.
Related
I added nvim-devicons and installed one of the patched fonts:
https://www.nerdfonts.com/font-downloads.
the icons still not showing,
Config with packer:
https://github.com/kyazdani42/nvim-web-devicons
use({
"kyazdani42/nvim-tree.lua",
requires = {
"kyazdani42/nvim-web-devicons", -- optional, for file icons
},
})
This is my GitHub repo for this config:
https://github.com/azizgharbi/Neovim-config
Should i install all the icons ? because i only installed one of those icons which is big blue terminal.
I am on macOs and using iterm2.
Make sure to set the terminal font to use the patched font.
To change the font in iTerm2:
Start iTerm2.
Click iTerm2 → Preferences → Profiles → Text.
Choose the patched font on the right.
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'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.
I'm currently loading two fonts from a qrc file. One of them is .ttf and the other is .otf. When I test my build on Android, they load and work fine (although the loading time is fairly long..)
However when I test it on my iPhone and/or simulator, none of the fonts load at all. They just seem to default back to Arial or something. Furthermore, only the .otf file works fine on Windows. The .ttf file defaults back to Arial and the characters are offset by 2 characters (so the word "ok" would be "qm")
What the hell is going on?
The fonts are located in:
"qrc:/fnt/res/fnt/MuseoSans_500.otf"
"qrc:/fnt/res/fnt/museo100-regular.ttf"
and I'm using this to load the fonts:
FontLoader {
id: museosans500
source: "qrc:/fnt/res/fnt/MuseoSans_500.otf"
}
FontLoader {
id: museo100
source: "qrc:/fnt/res/fnt/museo100-regular.ttf"
}
When accessing the fonts I set the font.family to museosans500.name and museo100.name. Like I said, they work fine on Android. But neither work on iOS, and only one of them works on Windows.
I'm using Qt Creator 3.4.0 and Qt 5.4.1.
Any help is much appreciated!
So I didn't solve it for Windows but this is an application for iOS and Android.
Basically I loaded the fonts through main.cpp instead with:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// Load custom fonts
if(QFontDatabase::addApplicationFont(QStringLiteral(":/fnt/res/fnt/MuseoSans_500.otf")) == -1)
qDebug() << "Failed to load font Museo Sans 500";
if(QFontDatabase::addApplicationFont(QStringLiteral(":/fnt/res/fnt/museo100-regular.ttf")) == -1)
qDebug() << "Failed to load font Museo 100";
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/res/qml/LoadScreen.qml")));
return app.exec();
}
Originally this didn't actually work. But that's because I was setting the font.family to be "Museo Sans 500" and "Museo 100" since that is what Windows would print in console when I asked for a full list of available font families.
When I printed out the font families list in Android, Mac and iOS I actually got "Museo Sans" and "Museo" respectively. Changing the font.family to those strings worked fine.
Unfortunately I have found no solution for Windows yet, but like I've said that doesn't matter to me.