Changing the text of UIButton programmatically in Swift


To change the text of a UIButton in Swift, you can use the setTitle() method of the button. This method takes an argument to set the button title for a particular state. Generally, we used the normal button state.

We will change the text of the button programmatically by following the below steps −

Step 1 − In this step, we will create two buttons (login and T&C) and add basic customization.

Step 2 − In this step, we will change the text of the login button.

Step 3 − In this step, we will change the text of the terms and conditions button.

Example

In this example, we will create two buttons. The first one is to set normal text and another one is for attributed text. In this step, we will add and customize the buttons. After that, we will add constraints to both buttons. Here is the code.

import UIKit
class TestController: UIViewController {    
   private let loginButton = UIButton()
   private let termsConditionButton = UIButton()    
   override func viewDidLoad() {
      super.viewDidLoad()
      initialSetup()
   }    
   private func initialSetup() {       
      // basic setup
      view.backgroundColor = .white
      navigationItem.title = "UIButton"
        
      // login button customization
      loginButton.backgroundColor = UIColor.gray
      loginButton.setTitleColor(.white, for: .normal)
      loginButton.layer.cornerRadius = 8
      loginButton.clipsToBounds = true
        
      // terms and conditions button customization
      termsConditionButton.backgroundColor = UIColor.gray
      termsConditionButton.setTitleColor(.white, for: .normal)
      termsConditionButton.layer.cornerRadius = 8
      termsConditionButton.clipsToBounds = true
        
      // adding the constraints to login button
      view.addSubview(loginButton)
      loginButton.translatesAutoresizingMaskIntoConstraints = false
      loginButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
      loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      loginButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
      loginButton.widthAnchor.constraint(equalToConstant: 280).isActive = true
        
      // adding the constraints to the terms & conditions button
      view.addSubview(termsConditionButton)
      termsConditionButton.translatesAutoresizingMaskIntoConstraints = false
      termsConditionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
      termsConditionButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
      termsConditionButton.widthAnchor.constraint(equalToConstant: 280).isActive = true
      termsConditionButton.topAnchor.constraint(equalTo: loginButton.bottomAnchor, constant: 30).isActive = true
   }
}

Output

In the above example, both buttons have been customized and added to the view by programmatic constraints. Now, we will set the text.

Change the text of the login button

In the below example, we will change the text using the setTitle() method. Here is an example.

Example

private func initialSetup() {
   // other statements
   setLoginButtonTitle()
}
private func setLoginButtonTitle() {
   loginButton.setTitle("Login", for: .normal)
}

Output

In the above code, we set the text of the button by calling the setTitle() method. In this method, we pass the button state for which you want to set the text. This is because there are different states available in the button element. But mostly we deal with the normal state. The button is in the default state of the button in normal mode. In the case of a different state, the same method will be used to set the text.

Change the text of the terms and conditions button

In this example, we will set the attributed title to the button. Here is an example.

Example

private func initialSetup() {
   // other statements
   setConditionsButtonTitle()
}
private func setConditionsButtonTitle() {    
   termsConditionButton.backgroundColor = .clear   
   let attributes: [NSAttributedString.Key: Any] = [
      .foregroundColor: UIColor.blue,
      .font: UIFont.systemFont(ofSize: 18, weight: .semibold),
      .underlineStyle: NSUnderlineStyle.single.rawValue
   ]
   let attributedText = NSAttributedString(string: "Terms & Conditions", attributes: attributes)
   termsConditionButton.setAttributedTitle(attributedText, for: .normal)
}

Output

In the above code, we set the attributed title to the button. We applied the foreground color and underline style to the button.

Conclusion

In this article, you learned about the setTitle() method that sets the text and attributed text. The same method can be used to apply the text. You can set the title for different states using the same method.

Updated on: 28-Feb-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements