How to add 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.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 07/13/2022 at 17:33
Last Updated on 08/27/2022 at 11:55
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.

Download Open Source projectHow to create a SwiftLint enabled Swift Xcode projectHow to create a LanguageCoordinator in Swift

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.

Discover TinyConstraintsAdd TinyConstraints to your project

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.

Download Open Source projectHow to localize iOS, MacOS, TVOS & WatchOS applications in XCode and Swift

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.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe