102

I have used a custom font in my previous app.

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

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

This worked perfectly.

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

Dude_Willie.otf
Impact
handsean.ttf

But I'm not sure how to load these.

I have tried

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

But this just falls back to using Helvetica.

How can I find what name to use?

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • possible duplicate of [Name of non-system font in iPhone](http://stackoverflow.com/questions/4143908/name-of-non-system-font-in-iphone) – borrrden May 28 '13 at 09:23
  • Possible duplicate of [Adding custom fonts to iOS app finding their real names](http://stackoverflow.com/questions/15984937/adding-custom-fonts-to-ios-app-finding-their-real-names) – Fyodor Volchyok Jun 01 '16 at 12:13
  • @FyodorVolchyok ah. Look at the comments. I disregarded that question already. The problem I was having was that I hadn't installed the fonts properly in the project so listing the `familyNames` didn't actually show the fonts I was looking for. – Fogmeister Jun 01 '16 at 12:37
  • https://unix.stackexchange.com/q/26053/26227 -- `fc-scan --format "%{foundry}: %{family}\n" filename.ttf` or `filename.otf`. – Dave Jarvis Dec 03 '20 at 01:43

15 Answers15

136

Right click on the TTF -> Get Info

"Full Name" is what you're looking for.

That's what worked for me with TTFs.

Edit:

I just used a font that had a different name from the "Full Name" in Get Info.

For the compilation of this answer, If the quick check above doesn't 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);
    }
}

And search for the correct name of the font 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)")
    }
}
ymutlu
  • 6,585
  • 4
  • 35
  • 47
Segev
  • 19,035
  • 12
  • 80
  • 152
  • It really depends on the maker of the font. We had some horrid fonts that we received from a client, and the font names are seriously arbitrary once they are consumed by iOS. I just tried this method with one of our fonts and it failed. – borrrden May 28 '13 at 09:29
  • @borrrden so far this method worked for me every time. I never created a font my self so you might be right, I don't know. – Segev May 28 '13 at 09:32
  • It will most likely work for well-made fonts. The fonts we got were created about 2 decades ago (it was bad...). However, the only way to be sure is to ask the system. – borrrden May 28 '13 at 09:35
  • @borrrden or to take 2 seconds to right click and try the name showing up there. :) If that doesn't work ~ then the digging starts.. – Segev May 28 '13 at 09:37
  • 3
    Thanks, this worked after I had properly installed the fonts in the info.plist file. – Fogmeister May 28 '13 at 09:41
  • Not sure if this is a hard and fast case, but if the 'Full Name' has spaces I've always had to replace them with a dash '-'. – Oliver Pearmain Jun 12 '14 at 10:18
  • ... or sometimes remove the spaces – Oliver Pearmain Jun 12 '14 at 10:49
  • 6
    This does not work for all fonts. For example, the "Full Name" value is shown as "Proxima Nova Regular" when the family in code is "ProximaNova-Regular" – rrbrambley Aug 14 '14 at 17:16
  • it's hilarious that i'm came here for this font family name question specifically for the "Proxima Nova Regular" font name... thank you @rrbrambley :) – Joris Weimar Jan 20 '17 at 13:45
  • I got the font name from the Swift 3 code. the Get Info was not the same – AbdulMomen عبدالمؤمن Jun 17 '17 at 05:34
  • @rrbrambley you should use PostScript name (open Font Book, load/open your font, then switch on info mode and check this PostScript name) – Damir Beylkhanov Aug 10 '17 at 14:12
  • get info seems to be a nice answer for people who are not aware native iOS development,. – Kartiikeya Mar 26 '18 at 08:57
102

Follow these four easy 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 project's info.plist, add a new entry for your_new_font.ttf or your_new_font.otf to the UIAppFonts array (plain text for this one is 'Fonts provided by application')
  • At this point, I recommend adding this temporary chunk of debug code to dump all fonts that are accessible by your app, including your newly added your_new_font:

//Swift

    for family: String in UIFont.familyNames {
        print("\(family)")
        for names: String in UIFont.fontNames(forFamilyName: family) {
            print("== \(names)")
        }
    }

//Objective-c

for(NSString *fontfamilyname in [UIFont familyNames]) {
    NSLog(@"family:'%@'",fontfamilyname);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) {
        NSLog(@"\tfont:'%@'",fontName);
    }
    NSLog(@"-------------");
}
  • In the debug output, look for your new font's 'family' and 'font' name. Pass whatever is displayed as the 'font' name corresponding to your new font family (there could be more than one 'font' associated with your new font 'family') to UIFont *myNewFont = [UIFont fontWithName:@"font_name_from_debug_output" size:20] and you should be in business!
Preetam Jadakar
  • 4,479
  • 2
  • 28
  • 58
John Jacecko
  • 1,870
  • 1
  • 16
  • 12
87
  • Install the font
  • Open Font Book app on your Mac
  • Select the font and click on 'info' button
  • The name you're looking for is PostScript name
    • MacOS: View -> Show Font Info

example

Cyrois
  • 439
  • 4
  • 9
Elin Gersin
  • 871
  • 6
  • 4
48

To use fonts in iOS, you have to load the font based on the font's FULL NAME (PostScript Name), which is sometimes (and usually is) different from the font's actual FILE NAME.

Imagine yourself renaming the Font file "Arial-regular.ttf" to be "foo.ttf". The font contained inside the font file you just renamed is still "Arial-regular".

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

If you are on a Mac or Linux, simply run this script from the command line in the directory where you have your custom fonts (uses the fc-scan utility from fontconfig which is probaly already installed, but if not you can 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 on my ~/Library/Fonts directory:

enter image description here

The script above will run through all the .ttf and .otf files in the current directory, then print out the PostScript Name for each font which you can use to reference the font file in XCode or elsewhere.

If you want to get fancy with some 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 a bit faster than copy-pasting code inside of your AppDelegate.m file to print out the names every time you want to add a new font file, which is the popular method, and it's also faster than opening the Font in FontBook to inspect the PostScript Name.

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

Hope this helps!

radiovisual
  • 6,298
  • 1
  • 26
  • 41
  • I am using Mac. I downloaded fontConfig from the link u provided. But it has so many files in it. How do I use it or install fontConfig to my mac. – Sujay U N Mar 24 '17 at 09:58
  • @SujayUN If you are on a Mac, you might already have the `fc-scan` utility installed. What happens when you run `fc-scan -V` on the terminal? – radiovisual Mar 24 '17 at 10:12
  • 3
    You can also install fontConfig via [homebrew](https://brew.sh/): `brew install fontconfig` . – radiovisual Mar 24 '17 at 10:14
  • I get this when I ran ur cmd -bash: fc-scan: command not found – Sujay U N Mar 24 '17 at 10:27
  • Ok, I recommend that you install via Homebrew if you don't have it installed on your machine. See the link to Homebrew and install command in my comment above. – radiovisual Mar 24 '17 at 11:08
  • Second script was just what I needed. Thanks! – squirtgun Dec 18 '18 at 20:41
10

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

import Foundation

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")
    }

    guard let font = CGFont(provider) else {
        throw Error("Could not load font")
    }

    guard let name = font.postScriptName else {
        throw Error("Could not get font name from font file")
    }

    return name as String
}

Replace with your own throwable Error objects as required.

Robert
  • 5,735
  • 3
  • 40
  • 53
9

Swift 4.0+ solution

    for fontFamilyName in UIFont.familyNames {
        print("family: \(fontFamilyName)\n")

        for fontName in UIFont.fontNames(forFamilyName: fontFamilyName) {
            print("font: \(fontName)")
        }
    }

Will output:

family: Apple SD Gothic Neo

font: AppleSDGothicNeo-Thin

font: AppleSDGothicNeo-Light

JaredH
  • 2,338
  • 1
  • 30
  • 40
6

You want to know how to get name go 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 it helps you...

Oliver Pearmain
  • 19,885
  • 13
  • 86
  • 90
Arpit Kulsreshtha
  • 2,452
  • 2
  • 26
  • 52
5

Swift 3.0

for familyName in UIFont.familyNames {
        for fontName in UIFont.fontNames(forFamilyName: familyName ) {
            print("\(familyName) : \(fontName)")
        }
    }
4

After you've added your fonts to your project/app, add this code (probably just in app delegate didFinishLaunchingWithOptions method) in order to print out all the available fonts for your app. From with-in that list you should be able to identify the font you're after. Don't forget to remove the unnecessary code after.

for (NSString *fontFamilyName in [UIFont familyNames]) {
    for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
        NSLog(@"Family: %@    Font: %@", fontFamilyName, fontName);
    }
}
Oliver Pearmain
  • 19,885
  • 13
  • 86
  • 90
4

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

Moritz
  • 1,590
  • 13
  • 8
3

Swift 1.2:

    for familyName in UIFont.familyNames() {
        for fontName in UIFont.fontNamesForFamilyName(familyName as! String) {
            println("\(familyName) : \(fontName)")
        }
    }
Harry Ng
  • 1,070
  • 8
  • 20
3

Another "quick, practical" hack that doesn't involve scanning through all the default fonts that exist on either the emulator or device is to use the font for something in your storyboard. Then if you edit the storyboard with a text editor (open as source code), you can see the fonts used with "internal" names if you search 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>
Essam Fahmi
  • 1,920
  • 24
  • 31
Kaolin Fire
  • 2,521
  • 28
  • 43
1

Log familyNames of font file and then access the fonts:

// You can log all font family names suing **fontNamesForFamilyName**

NSLog(@" font name %@", [UIFont fontNamesForFamilyName:@"the file name"]);

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
0

List font names and families for macOS with Swift 4.1:

NSFontManager.shared.availableFonts.map { print($0) }
NSFontManager.shared.availableFontFamilies.map { print($0) }
Ben Stahl
  • 1,139
  • 11
  • 11
-2

Swift 4.0 Supported

This is only one line code for print all font family and it's font's names

override func viewDidLoad() {
    super.viewDidLoad()
    //Main line of code
    UIFont.familyNames.sorted().forEach({ print($0); UIFont.fontNames(forFamilyName: $0 as String).forEach({print($0)})})
}
NiravS
  • 1,144
  • 1
  • 12
  • 24
  • Read the question. – Fogmeister Jan 18 '18 at 12:32
  • @Fogmeister Your accepted answer is same like this that's why I write this code in shorter way.. – NiravS Jan 19 '18 at 11:45
  • This line read the ttf or otf file and then give font family and it's all fonts name.. @Fogmeister – NiravS Jan 19 '18 at 11:47
  • If the accepted answer is the same as yours... why are you writing this one? Just edit the one that already exists (That's what the edit button is for). Don't try to steal other people rep points by copy pasting their answers for minor, insignificant changes. I have now changed the accepted question to a more recent one that shows how to properly install the fonts. Thanks – Fogmeister Jan 19 '18 at 11:56
  • Also, your answer is "only one line of code" but it is pretty much unreadable. I'd prefer an answer that has multiple lines that can easily be scanned over to see what it is doing. – Fogmeister Jan 19 '18 at 11:58