70

Say you want a specific font for UIFont. How do you know what it's called?

E.g. if you wanted to use this code:

[someUILabelObject setFont:[UIFont fontWithName:@"American Typewriter" size:18]];

From where do you copy the exact phrase "American Typewriter". Is there a header file in Xcode?

UPDATE

Also found this handy.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

6 Answers6

244

Might be interesting for you as Quick Win within the Debugger:

(lldb) po [UIFont fontNamesForFamilyName:@"Helvetica Neue"]

(id) $1 = 0x079d8670 <__NSCFArray 0x79d8670>(
HelveticaNeue-Bold,
HelveticaNeue-CondensedBlack,
HelveticaNeue-Medium,
HelveticaNeue,
HelveticaNeue-Light,
HelveticaNeue-CondensedBold,
HelveticaNeue-LightItalic,
HelveticaNeue-UltraLightItalic,
HelveticaNeue-UltraLight,
HelveticaNeue-BoldItalic,
HelveticaNeue-Italic
)

November 2018 - Update A new swift-y reference for "Custom Fonts with Xcode" - by Chris Ching. I had to update, as this is a great value posting for the new way combined with all missing parts to use custom fonts in a project.

andreas-supersmart
  • 4,114
  • 2
  • 18
  • 13
6

The documentation for UIFont is pretty clear on this:

You can use the fontNamesForFamilyName: method to retrieve the specific font names for a given font family. (Note: It is a class method)

You can get the family names like this:

NSArray *familyNames = [UIFont familyNames];
Alladinian
  • 34,483
  • 6
  • 89
  • 91
3
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
brian.clear
  • 5,277
  • 2
  • 41
  • 62
  • 2
    While this code sample may possibly answer the question, it would be preferable to include some essential explanation to your answer. As it stands now this answer adds little to no value for future readers. – oɔɯǝɹ Feb 05 '15 at 18:40
  • Disagree with previous commenter - this was what I needed. – codehearted Apr 29 '15 at 17:06
3

Try

NSArray *familyNames = [UIFont familyNames];
[familyNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
    NSLog(@"* %@",obj);
    NSArray *fontNames = [UIFont fontNamesForFamilyName:obj];
    [fontNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
        NSLog(@"--- %@",obj);
    }];
}];
Ezra
  • 45
  • 1
3

I made a library to solve this problem:

https://github.com/Nirma/UIFontComplete

All fonts are represented as a system Font enum and the library also details a way of using it with your custom fonts in the read me.

Basically this:

let font = UIFont(name: "Arial-BoldItalicMT", size: 12.0)

Is replaced with either this:

let font = UIFont(font: .arialBoldItalicMT, size: 12.0)

Or this:

let myFont = Font.helvetica.of(size: 12.0)
Nirma
  • 5,640
  • 4
  • 35
  • 47
2

This is how you get all font names in your project. That's it ... 3 lines of code

NSArray *fontFamilies = [UIFont familyNames];

    for (int i=0; i<[fontFamilies count]; i++)
    {
        NSLog(@"Font: %@ ...", [fontFamilies objectAtIndex:i]);
    }
Sam B
  • 27,273
  • 15
  • 84
  • 121