How to add Custom Fonts in iOS

In this article, I have explained how to add Custom fonts in iOS.  In the below image, You can see custom fonts added to iPhone Application.

 

Add Custom Font in iOS

Topics Covered:

1). Setting Up custom Fonts
2). Setting Custom fonts Programmatically to UILabel,UIButton,UITextField
3). Method to set custom font for all the elements in a UIViewController

1). Setting Up custom Fonts in iOS

I downloaded “OpenSans-Bold.ttf”,”SofadiOne-Regular.ttf” fonts from Google Fonts.

Follow the steps to add custom fonts to iPhone Application.

Step 1). Go to “Build Phases“, and Add custom font file to “Copy Bundle Resources” as shown in the below image.

Add Custom Font to Bundle

 

Step 2). Go to project’s Info.plist, create a key “Fonts provided by application” then add your fonts.

Add Custom Font in the Info.plist

Step 3). You can verify whether your font is ready for use. You can get  the full list of available fonts with [UIFont familyNames]

Check font is available with the following code

-(BOOL) checkFont:(NSString *)fontName
{
    NSArray *fonts = [UIFont familyNames];

   NSUInteger index = [fonts indexOfObject:fontName];

    if(index == NSNotFound)
        return NO;

    return YES;
}

Usage:

    if([self checkFont:@"Sofadi One"])
    {
        NSLog(@"Sofadi One is installed");
    }else
    {
        NSLog(@"Sofadi One is not installed");
    }
    if([self checkFont:@"Open Sans"])
    {
        NSLog(@"Opens Sans is installed");
    }else
    {
        NSLog(@"Opens Sans is not installed");

    }

2). Setting Custom font to UILabel,UIButton,UITextField

You can see in the below code, to set custom font.

//create font 
UIFont * font1 =[UIFont fontWithName:@"Open Sans" size:14.0f];

//set label font
self.label1.font =font1;

//set text field font
self.textFieldl1.font = font1;

//set button font
self.button1.titleLabel.font = font1;

 

3). Method to set custom font for all the elements in  UIViewController

You can use the below code, to set Custom Font for all the elements in a View Controller.

-(void) setCustomFont:(UIView *) view withFont:(UIFont *)font
{
    //go through all subviews
    for(UIView * subView in view.subviews)
    {
        if([subView isKindOfClass:[UILabel class]])
        {
            UILabel * label =(UILabel*)subView;
            label.font =font;
        }
        else if([subView isKindOfClass:[UITextField class]])
        {
            UITextField * textField =(UITextField*)subView;
            textField.font =font;
        }
        else if([subView isKindOfClass:[UIButton class]])
        {
            UIButton * button =(UIButton*)subView;
            button.titleLabel.font = font;
        }
        else if([subView isKindOfClass:[UIView class]])
        {
            //recursively.
            [self setCustomFont:subView withFont:font];
        }
    }
}

Usage:

UIFont * font =[UIFont fontWithName:@"Open Sans" size:15.0f];
[self setCustomFont:self.view withFont:font];

 

References: Apple Documentation