- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the font of UIButton with Swift?
In Swift, this is very easy to change the font of a button. You can use the titleLabel property of the button which is of type UILabel class. This property provides another property called font to apply the desired font. Let’s see some examples of changing the font.
We will follow the below steps to change the font of a button −
Step 1 − Initially, we will do the basic setup with button creation and customization.
Step 2 − In this step, we will change the system font with size and weight.
Step 3 − In this step, we will apply a custom font to the button.
Basic Setup
In the below example, we first create a button. After that, we customize the button to look better. In the last step, will add some required constraints to the button.
import UIKit class TestController: UIViewController { private let loginButton = UIButton() override func viewDidLoad() { super.viewDidLoad() initialSetup() } private func initialSetup() { // basic setup view.backgroundColor = .white navigationItem.title = "UIButton" // button customization loginButton.backgroundColor = UIColor.gray loginButton.setTitle("Login", for: .normal) loginButton.setTitleColor(.white, for: .normal) loginButton.layer.cornerRadius = 8 loginButton.clipsToBounds = true // adding the constraints to 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 } }
Output
You can see the default look of the button without changing the font size or style in the above output.
In real iOS applications, you may need to change the font size and style. Let see how to implement that in the next step
Change the system font of the button
We will change the font size along with the font weight in the above example by adding the following code to it.
loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
Output
Apply the custom font to the button
loginButton.titleLabel?.font = UIFont.init(name: "AmericanTypewriter", size: 18)
Output
You should note that here that it is required to have the font file in your project before using that font in the code. Also after adding the font file, it is required to add the “Info.plist” file.
Conclusion
In conclusion, we can apply the system font with different weights to the button. Also, if you want to apply the custom font, you can pass the font’s name with size. Generally, we used the titleLabel property of the button to change the font.