Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

<< Back to SWIFT

Swift UIFont Example: Name and Size

UIFont. Times New Roman, Helvetica, San Francisco, Comic Sans: these are fonts. For a professional program design, a good font is essential.
But fonts can convey meaning. For example Times New Roman looks serious. Comic Sans looks childlike. In Swift we apply the UIFont class to change fonts dynamically.
To begin, please add a Text Field to your iPhone program. Then add an outlet to it in your ViewController.swift file. Control click and drag it.UITextField

Next: We want to change the font property of our UITextField. We create a UIFont with a name and size argument.

UIFont: Some init methods for UIFont return an optional UIFont. So we can make sure they are valid with an if-let statement.

Optionals: if let
Example UIFont, font property: Swift // // ViewController.swift // ExampleTime7 // // ... // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Create UIFont with Times New Roman. // ... Set size to 19.0. let font = UIFont(name: "Times New Roman", size: 19.0) // Use font on UITextField. simpleText.font = font } @IBOutlet weak var simpleText: UITextField! override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
Run simulator. When we run the simulator, we see the result of our work. We must type some text into the Text Field. The text should have a serif font.
A review. With methods on the UIFont class we look up font objects. But we can also create new UIFont instances with the init method.
The Dev Codes