Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 780
tika Avatar asked Oct 14 '16 04:10

tika


People also ask

How do I change the font in Xcode storyboard?

In the storyboard , on the NavigationBar go to the Attribute Inspector,click the right icon button of the Font select area.In the popup panel , choose Font to Custom, and choose the Family of you embeded font name.

How do I change the default font in Xcode?

Add the Font File to Your Xcode Project To add a font file to your Xcode project, select File > Add Files to “Your Project Name” from the menu bar, or drag the file from Finder and drop it into your Xcode project. You can add True Type Font (. ttf) and Open Type Font (. otf) files.

What is the default font family in Xcode?

Helvetica Neue was the default font for iOS 8 and prior. Apple has changed its default font from iOS 9 to San Francisco. 1) For your app If you want consistent font for all iOS versions then you have to explicitly set the San Francisco font via Storyboard or code.


1 Answers

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

}
like image 135
Carien van Zyl Avatar answered Sep 27 '22 22:09

Carien van Zyl