14

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tika
  • 7,135
  • 3
  • 51
  • 82
  • 1
    Sorry but you have to set manually font of all label, button etc. If you make one globalConstants.h file declare one variable of font like let let ICON_FONT = UIFont(name: "OpenSans-Bold" size: 30) and manually give font to each label. so now if you change font, Just one change in globalConstant file. – Jitendra Modi Oct 14 '16 at 04:56

1 Answers1

9

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)
    }

}
Community
  • 1
  • 1
Carien van Zyl
  • 2,853
  • 22
  • 30
  • 1
    But In case of custom `MyUILabel`, you will have to change the class of all UILabels, Right? Isn't it as repeated as to changing font of all labels? – tika Oct 14 '16 at 05:43
  • 1
    Isn't this answer just a more detailed explanation of OP's no. 2? Except for the IB part, but like tika said there's not much value added. – HAS Oct 14 '16 at 05:50
  • @tika It might be as repetitive (although my opinion is that it is faster to change the class than the font + size), but what you do win, is that when you want to change the font of the entire app, you do it in one place. – Carien van Zyl Oct 14 '16 at 06:44