UIFont fontWithName font name

Ios5Uifont

Ios5 Problem Overview


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.

Ios5 Solutions


Solution 1 - Ios5

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.

Solution 2 - Ios5

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];

Solution 3 - Ios5

label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];

Solution 4 - Ios5

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);
    }];
}];
    

Solution 5 - Ios5

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)

Solution 6 - Ios5

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]);
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSnowcrashView Question on Stackoverflow
Solution 1 - Ios5andreas-supersmartView Answer on Stackoverflow
Solution 2 - Ios5AlladinianView Answer on Stackoverflow
Solution 3 - Ios5brian.clearView Answer on Stackoverflow
Solution 4 - Ios5EzraView Answer on Stackoverflow
Solution 5 - Ios5NirmaView Answer on Stackoverflow
Solution 6 - Ios5Sam BView Answer on Stackoverflow