How to add a label to a view in Swift

Oscar de la Hera Gomez
A flower that represents Swift next to a flower that represents XCode. Beneath it sits the text that states 'Adding a label to a View in Swift'.

A step by step tutorial on creating a label and adding it to a UIView in Swift (iOS). Github repository included.

The following article walks you through how to create a label and apply content from our LanguageCoordinator. This builds on our existing SwiftLint starter project and the LanguageCoordinator walkthroughs.

Step One: Create your ViewController UI extension

A screenshot on how to create the ViewController+UI.swift file.

Select the RootViewController folder and press Command + N.

This will select the Swift, iOS file type. Press Next.

After that enter the name of your file - ViewController+UI - and press Create.

Step Two: Create your extension and setup UI function

A screenshot showing you the code for the new ViewController+UI extension.

Copy the code below into your ViewController+UI.swift file.

Step Three: Call your setupUI function

A screenshot showing you how to call the setupUI function from the ViewController ViewDidLoad function.

In your ViewController.swift file, call self.setupUI() from ViewDidLoad.

Step Four: Create your label

A screenshot showing you how to create a UILabel constant.

Under // MARK: UI create your label as shown above.

Step Five: Add your Label to your view controller

A screenshot showing you the code we used to add the label to the UIView within the ViewController. Please note we used TinyConstraints to ease our use of Constraints.

In ViewController+UI.swift create a private function called setupLabel and add the label to the view.

In the example code below, we use TinyConstraints to simplify how we layout UI - in this case a label - in our applications.

Step Six: Set your label text

A screenshot showing you how to update your label text when the content is available.

In ViewController.swift update the label text within the LanguageCoordinator.onContentUpdate callback.

Step Seven: Verify

A screenshot of our app on an iPhone 13.

Run your app and verify the text shows up.

Any Questions

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details