X Pahadi
X Pahadi

Reputation: 7443

XCode - Change Default Global Font in Storyboard

Very much like how we can set the Global Tint in Storyboard, Is it possible to set the global font-family as something else? For example, I want to change the Global/Default Font from System to Source Sans Pro 16pt. However, what I have to do (to my knowledge) is one of the following:

  1. Change font of each label, button, textField, etc. in Storyboard.
  2. Set it via Swift ViewDidLoad Code (like this question) or through extensions as explained in this question

My Problem with (2) is that I do not get Visual Feedbacks like in (1) using storyboards. On the other hand, it is also not very eloquent as I have to manually set it anyway.

So, is there a way to change/set the default Storyboard font?

Upvotes: 15

Views: 10120

Answers (1)

Carien van Zyl
Carien van Zyl

Reputation: 2873

You can use the Appearance API, to ensure that all controls have the same font (at runtime)

Look at this question as to how to set the font for UIButton with Appearance.

For UILabel and UITextView, do the following. This can be done in AppDelegate application(_:didFinishLaunchingWithOptions:)

let labelAppearance = UILabel.appearance()
labelAppearance.font = UIFont.myFont()

let textFieldAppearance = UITextView.appearance()
textFieldAppearance.font = UIFont.myFont()

The previous solution, however will not update storyboard.

To see the changes visually on storyboard, you can look into the function

prepareForInterfaceBuilder()

Here is an answer that explains how to get it visually updated in storyboard, but for this you will need to use custom classes for your textFields, buttons, etc.

Code Example as per above link:

@IBDesignable
public class MyUILabel: UILabel {

    public override func awakeFromNib() {
        super.awakeFromNib()
        configureLabel()
    }

    public override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        configureLabel()
    }

    func configureLabel() {
        font = UIFont(name: Constants.DefaultFont, size: 40)
    }

}

Upvotes: 9

Related Questions