Table of contents
1.
Introduction
2.
iOS Label
3.
Adding iOS Label
3.1.
Using Storyboard
3.2.
Using Code
3.2.1.
Output
4.
Customizing iOS Label
4.1.
Using Storyboard
4.2.
Using Code
5.
Frequently Asked Questions
5.1.
What is the iOS Label?
5.2.
What is the parent class of iOS Label?
5.3.
How to change the color of the text in the Label?
5.4.
What is lineBreakStrategy property in Label?
5.5.
How to fit text inside a label's bounding rectangle?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

iOS Label

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

iOS is an operating system developed by Apple for their hardware. After Android, iOS is the second most popular mobile operating system worldwide. For making iOS apps and accessories for these devices, Apple offers information and tools, and for that, you need a Mac running the most recent version of Xcode.

iOS Label

An iOS Label is a fundamental object in iOS development used almost everywhere. It is a basic UI control that displays plain static or styled text. 

In this article, we will discuss iOS Label, what class it inherits, how to add it to an iOS application using storyboard and code, and how to change the different properties of a Label. So, let's begin.

iOS Label

A label is a view that displays one or more lines of informational text. The Label is used in iOS applications in conjunction with UIControls to fulfill the Application requirements.

Labels provide valuable information to the users. It is recommended to internationalize the text and support accessibility in the labels to make sure that information reaches a broad audience. The default accessibility traits for a label are Static Text and User Interaction Enabled.

Labels are a child class of the UIView class, and it is declared as given below.

@MainActor class UILabel : UIView

If you don't know about the UIView class, then don't worry. We got you covered!

UIView is a class using which we can create and manage a rectangular area on the screen and the contents inside it. We can have any number of views inside a view. It creates a hierarchical structure of the UIViews.

A view object renders content within its bounds rectangle and handles any interactions with that content.

Now, let's return to iOS Label and learn how to add it to our application.

Adding iOS Label

You can add iOS Label to your application in two ways, first, by using a storyboard, and second, by writing code. We will discuss adding a tab bar in our application using both methods. Before proceeding further, ensure you have created a new application in XCode and follow the steps below.

Using Storyboard

Follow the steps below to add the iOS Label on your view controller.

  1. Click on the + icon on your screen's top right corner. This will open the object library.
  2. Search for Labels on the search bar. The Label object will appear.
  3. Drag and drop the Label object on your screen, as shown in the picture below.
XCode screenshot

Using Code

To add a Label to your application using code, you can refer to the code below. Paste this code in the viewController file of your application to see the changes for yourself.

import UIKit
class ViewController: UIViewController {
   override func viewDidLoad() {
       super.viewDidLoad()
       let label = UILabel(frame: CGRect(x:0 , y:0 , width:250 , height:50))
       label.center = CGPoint(
           x: self.view.frame.size.width/2,
           y: self.view.frame.height/2
       )
       label.text = "Namaste!"
       label.textAlignment = .center
       label.backgroundColor = .black
       label.textColor = .white
       label.font = label.font.withSize(50)
       
       self.view.addSubview(label)
   }
   
}

Output

The above code produces the output given below.

Output screenshot

Customizing iOS Label

Similar to adding iOS Labels to your application, customizing them can be done in two ways. And we are going to discuss both methods as before.

Using Storyboard

Customizing labels using a storyboard is easy. You have to click on the Label you want to customize; it will show a tab on the right side in XCode, which contains several options to customize the selected Label. A snapshot of the available properties is given below.

XCode screenshot

Using Code

You can alter several properties to customize the text displayed by Label. Below are some important properties that will be handy in your iOS development journey.

Properties table

From the properties given above, the attributedText property allows you to control the properties of individual characters or groups of characters. It uses the NSAttributed String API to do so.

Contrary to this, if you want to format the label's text uniformly, it can be done by setting the text property to an NSString object.

Apart from the properties mentioned above, a few more properties may be handy, which are given below. 

If you want to limit the number of lines on which a label's text can be displayed, then you can lay out the text with the numberOfLines property. If this value is set to 0, the label can use as many lines as necessary.

lineBreakMode property controls how the label splits the text into multiple lines and the truncation behavior associated with the final line.

You can use Auto Layout to size the label according to the text. By default, it sets the content size for a label to display the entire text on a single line. If you provide the constraints only for the width of the label but not the height, then the label's intrinsic content size adjusts the height to display the text completely.

If you define the size of the label by yourself, then what happens if the content doesn't fit within the bounds that you have defined? For this problem, you can set the adjustsFontSizeToFitWidth property to true, which will fit the label's contents within its bounds.

allowsDefaultTighteningForTruncation property is another unique property that instructs the label to reduce the spacing between characters before truncating the string. It's similar to the Mumbai local train, where more people are trying to fit into a small space 😂.

Frequently Asked Questions

What is the iOS Label?

iOS Label is a view that displays one or more lines of informational text.

What is the parent class of iOS Label?

Labels are a child class of the UIView class.

How to change the color of the text in the Label?

The color of the Label can be customized by changing the textColor property.

What is lineBreakStrategy property in Label?

Line-break strategies are collections of options the system uses to determine where to break lines in a paragraph.

How to fit text inside a label's bounding rectangle?

You can set the adjustsFontSizeToFitWidth property to true to fit the text inside the bounding rectangles of the Label.

Conclusion

This article taught us about Labels in iOS and how we add them to our application. We also looked into changing its appearance using storyboard and swift. If this was your first article about learning iOS development and you needed help understanding some things, don't worry. You can refer to this article to help you create your first iOS app.

We hope this blog has helped you enhance your iOS app knowledge. Here is a list of articles that can help you learn more about ios development.

Refers to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning Ninja! 🥷

Live masterclass