How to get font name from otf or ttf file?

I used my own font in my previous application.

The file name was "ProximaNova-Regular.otf" and load the font that I used ...

[UIFont fontWithName:@"ProximaNova-Regular" size:20]; 

It worked perfectly.

Now in this new application I have three font files ...

 Dude_Willie.otf Impact handsean.ttf 

But I'm not sure how to download them.

I tried

 [UIFont fontWithName:<the file name> size:20]; 

But it just goes back to using Helvetica.

How to find out which name to use?

+74
ios uifont
May 28 '13 at 9:19
source share
12 answers

Follow these four simple steps to add and use a new font in your iOS app:

  • Add your_new_font.ttf or your_new_font.otf to your Xcode project
  • In your info.plist project, add a new entry for your_new_font.ttf or your_new_font.otf to the UIAppFonts array (the plain text for this is the Fonts provided by the application ')
  • At this point, I recommend adding this temporary piece of debugging code to reset all the fonts available to your application, including the recently added your_new_font:
 for(NSString *fontfamilyname in [UIFont familyNames]) { NSLog(@"family:'%@'",fontfamilyname); for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) { NSLog(@"\tfont:'%@'",fontName); } NSLog(@"-------------"); } 
  • In the debug output, find the new name 'family' and 'font'. Pass everything that appears as the name of the β€œfont” corresponding to your new font family (there can be more than one β€œfont” associated with your new font family) up to UIFont *myNewFont = [UIFont fontWithName:@"font_name_from_debug_output" size:20] , and you must be in business!
+62
Jul 15 '13 at 22:43
source share

Right click on TTF β†’ Get Info

The "full name" is what you are looking for.

What worked for me with TTF.

Edit:

I just used a font with a different name from "Full Name" in Get Info.

To compile this answer, if the quick check above does not work, run this code in your project:

 for (NSString *fontFamilyName in [UIFont familyNames]) { for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) { NSLog(@"Family: %@ Font: %@", fontFamilyName, fontName); } } 

Look for the correct font name that you want to use.

Swift 3.0 Code:

 for fontFamilyName in UIFont.familyNames{ for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){ print("Family: \(fontFamilyName) Font: \(fontName)") } } 
+117
May 28 '13 at 9:27
source share
  • Set font
  • Open the Font Book app on your Mac.
  • Select a font and click on the "Information" button
  • The name you are looking for, PostScript Name

example

+44
Aug 01 '17 at 17:06 on
source share

To use fonts in iOS, you need to download a font based on the FULL NAME (PostScript Name) font, which sometimes (and usually) differs from the actual FILE NAME file name.

Imagine that you are renaming the font file "Arial-regular.ttf" to be "Foo.ttf". The font contained within the font file that you just renamed is still "Arial-regular."

There are some good programmatic ways to get the font name already in this thread, but I have a different approach using the command line.

If you are on Mac or Linux, just run this script from the command line in the directory where you have your own custom fonts (uses the fc-scan utility from fontconfig , which is already installed, but if you cannot install it via homebrew: brew install fontconfig ):

 for file in "$arg"*.{ttf,otf}; do fc-scan --format "%{postscriptname}\n" $file; done 

Here is a screenshot of the above command running in my ~/Library/Fonts directory:

enter image description here

The script above will go through all the .ttf and .otf in the current directory and then print a PostScript Name for each font that you can use to refer to the font file in Xcode or elsewhere.

If you need additional information (PostScriptName, Filename) and some color coding, you can run this alternative script:

 for file in "$arg"*.{ttf,otf}; do postscriptname=$(fc-scan --format "%{postscriptname}\n" $file); printf "\033[36m PostScript Name:\033[0m %s \e[90m(%s)\033[0m\n" "$postscriptname" "$file"; done 

enter image description here

This is slightly faster than copying the code inside the AppDelegate.m file to print the names every time you want to add a new font file, which is a popular method, and also faster than opening a font in FontBook to check the PostScript name.

USEFUL TIP: If you use the above script alias in your terminal, so all you need to do is enter a single command to get all PostScript font names for all the files in the current one (my function is called fontnames , so all I need to do , this is the type of fontnames on the terminal inside the directory with the fonts in it, and the PostScript names will be printed automatically, then you will save time in your development workflow and this convenient script is ready to use when you need it.

Hope this helps!

+32
Jan 13 2018-01-17
source share

You want to know how to get a name for this: -

  NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]]; for (NSInteger indFamily=0; indFamily<[familyNames count]; ++indFamily) { NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]); NSArray *fontNames = [[NSArray alloc] initWithArray: [UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]]; for (NSInteger indFont=0; indFont<[fontNames count]; ++indFont) { NSLog(@" Font name: %@", [fontNames objectAtIndex:indFont]); } } 

hope this helps you ...

+6
May 28 '13 at 9:28
source share

Swift 3.0

 for familyName in UIFont.familyNames { for fontName in UIFont.fontNames(forFamilyName: familyName ) { print("\(familyName) : \(fontName)") } } 
+5
Oct 15 '16 at 9:28
source share

After you have added your fonts to your project / application, add this code (perhaps only to the delegate didFinishLaunchingWithOptions application) to print all available fonts for your application. From list "B" in this list, you can determine the font that you need. Remember to delete the unnecessary code after.

 for (NSString *fontFamilyName in [UIFont familyNames]) { for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) { NSLog(@"Family: %@ Font: %@", fontFamilyName, fontName); } } 
+4
Jun 12 '14 at 13:27
source share

If you want to find the font name for a given font file programmatically:

 func loadFontName(for file: URL) throws -> String { let data = try Data(contentsOf: file) guard let provider = CGDataProvider(data: data as CFData) else { throw Error("Could not create data provider") } let font = CGFont(provider) guard let name = font.postScriptName else { throw Error("Could not get font name from font file") } return name as String } 

Replace your own objects with the ability to move Error as needed.

+4
Nov 01 '16 at 12:15
source share

You can also use otfinfo --info arial.ttf to get the name from the command line. The postscript name is the one you need to pass to the UIFont constructor.

+3
Feb 24 '15 at 10:50
source share

Swift 1.2:

  for familyName in UIFont.familyNames() { for fontName in UIFont.fontNamesForFamilyName(familyName as! String) { println("\(familyName) : \(fontName)") } } 
+3
May 29 '15 at 2:23
source share

Record the file name of the font file name and then access the fonts:

 // You can log all font family names suing **fontNamesForFamilyName** NSLog(@" font name %@", [UIFont fontNamesForFamilyName:@"the file name"]); 

Hope this helps you.

+1
May 28 '13 at 9:30
source share

Another β€œquick, practical” hack that does not include checking all the default fonts that exist on the emulator or device is to use the font for something in your storyboard. Then, if you are editing the storyboard using a text editor, you can see the fonts used with the "internal" names if you are looking for the "customFonts" tag:

 <customFonts key="customFonts"> <array key="Linotype - AvenirNextLTPro-Bold.otf"> <string>AvenirNextLTPro-Bold</string> </array> <array key="Linotype - AvenirNextLTPro-Regular.otf"> <string>AvenirNextLTPro-Regular</string> </array> <array key="TradeGothicLTPro-BdCn20.otf"> <string>TradeGothicLTPro-BdCn20</string> </array> </customFonts> 
+1
May 01 '17 at 9:28 pm
source share



All Articles