I want to use HelveticaNeue-UltraLight in my ios application. I\'m adding the font file as a resource to my project and adding the \"Fonts provided by application\" key in t
Not only does the file needs to be added to the project, but it needs to be added to the target as well.
Sometimes (as in my case), when drag and dropping the ".ttf" file in Xcode, the "add to target" is not checked. This means the file is actually visible in your project, but it is not embedded in the app.
To make sure it is:
You should see the font file in that list.
Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.
It should work even for different font variations of the same family.
After spending hours, I came to know that the Name for the font we need to add is not the file name of the font but the actual Font name. I was trying to include font file bonveno.ttf. I have included the filename along with extension in info.plist and was able to see the font in the Copy Bundle Resources list. My code was like
self.label.font = [UIFont fontWithName:@"bonveno" size:30];
that was causing the problem. Then I double clicked the font file from Finder to see the preview of the font. That time I noticed the name of the font in the preview window as BonvenoCF. So I added that name in the code like
self.label.font = [UIFont fontWithName:@"BonvenoCF" size:30];
Worked like charm!! Can't live without this cute font in my apps.
In order to use custom fonts in iOS, just add your fonts by drag & drop to your project. Mention the array of font names with extension in Info.plist with key "Fonts provided by application". Then print all font family names using below code :
for (NSString* family in [UIFont familyNames])
{
DebugLog(@"FONT FAMILY: %@", family);
for (NSString *name in [UIFont fontNamesForFamilyName: family])
{
DebugLog(@" %@", name);
}
}
Console print :
Arial Hebrew ArialHebrew-Bold ArialHebrew-Light ArialHebrew
Calibri Calibri-Bold Calibri
Georgia Georgia-BoldItalic Georgia-Bold Georgia-Italic Georgia
Then from console, you will get the actual font-names (As above). Use below code to create font -
In core text :
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);
In UIKit :
UIFont *font = [UIFont fontWithName:fontName size:10];
Note : here fontName = @"Calibri-Bold" is without extension.
Another thing to check is on a Mac: Open the app Font Book. Then go file -> file validation and open the font. If something is failing (even with a warning) in there iOS generally rejects loading the font in the app with no errors.
If this is the case then you can fix the errors by opening the font in a free app called "Typelight" And resaving the font "Save as".
Target membership
is enabled, if not enable it. Build phases
> under Copy Bundle Resources
verify all your fonts are listed. If not, add one by one using +
icon. Fonts provided by application
> add your fonts one by one as Helvetica-Bold.ttf
.