Skip to content
DeveloperMemos

How to Change the Font Size of a UILabel in Swift

Swift, iOS, UILabel2 min read

UILabel is a commonly used class in iOS app development that is used to display text on the screen. In Swift, setting the font size of a UILabel is a relatively simple task. In this article, we will discuss various methods to set the font size of a UILabel in Swift.

Method 1: Using the Interface Builder

The Interface Builder is a graphical tool in Xcode that allows you to design the user interface of your app. You can use the Interface Builder to set the font size of a UILabel. Follow the steps below to set the font size of a UILabel using the Interface Builder:

  1. Open the storyboard or nib file that contains the UILabel you want to modify.
  2. Select the UILabel and go to the Attributes inspector in the Utilities panel.
  3. In the Attributes inspector, locate the Font section and click on the disclosure triangle to reveal more options.
  4. In the Size field, enter the desired font size. You can also use the stepper control to increase or decrease the font size.
  5. Once you have set the font size, you can run your app to see the changes.

Method 2: Using Code

You can also set the font size of a UILabel programmatically using code. Follow the steps below to set the font size of a UILabel using code:

  1. Create an outlet for the UILabel in your view controller.
1@IBOutlet weak var myLabel: UILabel!
  1. In the viewDidLoad() method of your view controller, set the font size of the UILabel using the font property.
1override func viewDidLoad() {
2 super.viewDidLoad()
3 myLabel.font = UIFont.systemFont(ofSize: 20)
4}

In the example above, we set the font size to 20 points using the systemFont(ofSize:) method of the UIFont class.

You can also set the font size of a UILabel using a custom font. To do this, you will need to first add the custom font to your project and then use the font(withName:size:) method of the UIFont class to create a UIFont object with the desired font and size.

1if let customFont = UIFont(name: "MyCustomFont", size: 20) {
2 myLabel.font = customFont
3}

In the example above, we create a UIFont object using the custom font "MyCustomFont" with a size of 20 points.

Conclusion

In this article, we discussed two methods to set the font size of a UILabel in Swift. You can use the Interface Builder to set the font size of a UILabel graphically, or you can set it programmatically using code. Setting the font size of a UILabel is a simple task that you can perform easily using either method.