How to Make Part of UILabel Text Bold or Change Color (Swift)

Here, we will explain how to make part of the text in a UILabel bold or change its color using Swift.

How to Make Part of a UILabel Bold or Change Color in Swift 1

Create a Test iOS App

First, let's create a simple iOS app that only displays a UILabel for testing purposes.

If you don't know how to create a new app, add buttons, or create actions, please refer to How to Create a Basic iOS App.

In Xcode, create a new project with [iOS] > [App].

Place a UILabel on the Main Storyboard's View Controller. The position doesn't matter.

How to Make Part of a UILabel Bold or Change Color in Swift 2

How to Make Part of a UILabel Bold or Change Color in Swift 3


Create an outlet from the Label and name it itemNameLabel.

How to Make Part of a UILabel Bold or Change Color in Swift 4

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var itemNameLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

How to Make Part of a UILabel Bold or Change Color in Swift

Now, let's write the code to make part of the UILabel bold or change its color in Swift.

In this example, we will display the header in bold, followed by an item name, and finally an asterisk (*) in red within a single label (itemNameLabel).


To change the font or color of text in a UILabel, we use NSAttributedString and NSMutableAttributedString, and then assign them to the label's attributedText property.

Update ViewController.swift as follows:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var itemNameLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let headerText = "Item"
        let valueText = "Item Name ABC"
        let colorText = "*"
        
        if let headerFont = UIFont(name: "Verdana Bold", size: 17),
           let valueFont = UIFont(name: "Verdana", size: 17) {
            
            let asHeader = [NSAttributedString.Key.font : headerFont]
            let asValue = [NSAttributedString.Key.font : valueFont]
            let asColorRed = [NSAttributedString.Key.font : valueFont, NSAttributedString.Key.foregroundColor: UIColor.red]
            
            let maString = NSMutableAttributedString(string: "\(headerText): ", attributes: asHeader)
            maString.append(NSMutableAttributedString(string: "\(valueText) ", attributes: asValue))
            maString.append(NSMutableAttributedString(string:  "\(colorText)", attributes: asColorRed))
            
            itemNameLabel.attributedText = maString
        } else {
            itemNameLabel.attributedText = NSMutableAttributedString(string: "\(headerText): \(valueText) \(colorText)");
        }
    }
}

How to Make Part of a UILabel Bold or Change Color in Swift 5


Explanation of the code:

Lines 10 - 12: Define the strings to be displayed in the label, separating them into header, value, and colored text parts.

In this example, we use Verdana Bold for the header and Verdana for the value, both with font size 17.

Lines 14 - 15: Create the UIFont objects using if let. If font creation fails, the else clause at line 27 executes and displays plain text without attributes.

Line 17: Create an attributed string for the header with Verdana Bold / size 17.

Line 18: Create an attributed string for the value text with Verdana / size 17.

Line 19: Create an attributed string for the color text with Verdana / size 17 and red color.

Line 21: Initialize a NSMutableAttributedString with the header text and its attributes.

Line 22: Append the value text with its attributes to maString.

Line 23: Append the color text with its attributes to maString.

Line 25: Assign maString to the label's attributedText to display styled text.

The above is the code for making part of a UILabel bold or changing its color in Swift.


Test the iOS App in the Simulator

Now, let's build and run the app in the simulator.

How to Make Part of a UILabel Bold or Change Color in Swift 6


The label text is displayed with “Item:” in bold and “*” in red.

How to Make Part of a UILabel Bold or Change Color in Swift 7


As a test, change headerFont and asColorRed as follows:

if let headerFont = UIFont(name: "Copperplate Bold", size: 22),
let asColorRed = [NSAttributedString.Key.font : valueFont, NSAttributedString.Key.foregroundColor: UIColor.blue]

When you run the app again, the header font changes, and the asterisk appears in blue.

How to Make Part of a UILabel Bold or Change Color in Swift 8


That's how you make part of a UILabel bold or change its color using Swift.