How To Add Custom Font From TTF File To iOS App In Xcode

This article will tell you how to add text font library from external .ttf  font file to swift Xcode project, and how to use the .ttf file provided text font for label text in swift source code.


1. Add Third Party Text Font From TTF File To Swift Xcode Project Steps. 

Before you can use the text font in ttf file, you need to get a ttf file and add it to your Xcode project follow below steps.

  1. Search keyword free Mac fonts in google and download free ttf font files. Generally it is a zip file.
  2. Unzip above zip file to get the .ttf file. This file contains the text font.
  3. Create a Xcode project with name SwiftFontFromTTFFileExample.
  4. Right click the project folder in Xcode, then click Add Files to … menu item in the popup menu list.
    add files to swift xcode project
  5. Select the downloaded .ttf file in the popup file browser and click Add button to add it to the project files list.
    select the font ttf file to add it toswift xcode project
  6. Now you can see the .ttf file has been added in the Xcode left panel project files list. In this example, the ttf file is Sweet Hipster.ttf.
    the ttf file has been added in xcode project file list

2. Add .ttf Font File To Info.plist File Steps.

After add the .ttf file to your Xcode project, you need to add the .ttf file name in Xcode project’s Info.plist file, then you can use text font defined in the .ttf file in your swift source code.

  1. Click Info.plist file in Xcode project. Then in Xcode right panel, move you mouse on the Information Property List item, now you will see a plus icon at the end of Information Property List item, click the plus icon to add one property in the list. Select Fonts provided by application option item from the drop down list.
    add text font from ttf file to ios xcode project
  2. Click the triangle icon at the beginning of Fonts provided by application item to expand it, and input the .ttf file name with .ttf file extension in the Value column text box.
    input ttf file name in fonts provided by application item input text box
READ :   How To Make Swift Buttons Auto Layout To Full Fill Screen Width Responsively In iOS

3. Use .ttf File Text Font In Swift Source Code.

Now you can use the added text font from .ttf file in your swift source code like below, before look at the source code, let us first see the result picture.

label text use text font from ttf file

Below is the the source code of ViewController.swift file. If you want to see the detail example about how to change label text font, please read article How To Customize Swift Label Text Font And Set Text Shadow.

//
//  ViewController.swift
//  SwiftFontFromTTFFileExample
//
//  Created by song zhao on 7/4/19.
//  Copyright © 2019 dev2qa.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // First print out all the os font names, you can see the added font name from .ttf file in the returned array.
        print(UIFont.familyNames)
        
        // Create a UILabel object.
        let label: UILabel = UILabel()
        
        // Set label text.
        label.text = "Text font from ttf file. "
        
        // Get screen width and height.
        let screenWidth = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        // Set label position and width, height.
        label.frame = CGRect(x:100, y: screenHeight/2, width: screenWidth - 100, height: 100)
        
        // Make the label locate at screen center.
        label.center = CGPoint(x: screenWidth/2, y: screenHeight/2)
        
        // Align the label text at center.
        label.textAlignment = NSTextAlignment.center
        
        // Set label text font to use the text font from .ttf file.
        label.font = UIFont(name: "Sweet Hipster", size: 60)
        
        // Add the label to main screen.
        self.view.addSubview(label)
        
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.