Xcode 6
Size Classes
Custom Fonts
Font Sizing Issue
iOS Development

Custom Font Sizing in Xcode 6 Size Classes not working properly with Custom Fonts

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When custom fonts do not respond to size classes the way system fonts seem to, the usual problem is a mismatch between interface adaptation and font assignment. Size classes help choose layouts, but they do not automatically make custom fonts scale intelligently across all device contexts.

Why Custom Fonts Feel Different

System fonts integrate cleanly with Apple's text styles and sizing behaviors. Custom fonts do not automatically inherit that same adaptive behavior, especially in older Xcode workflows where font size was often stored as a fixed storyboard value per view.

That leads to a common surprise:

  • the layout adapts correctly
  • constraints switch as expected
  • the custom font still looks too small or too large in one size class

The reason is that size classes are primarily about layout variation. Font adaptation with custom fonts often needs explicit code or carefully separated storyboard settings.

Set Custom Fonts Programmatically by Trait Collection

When the custom font must change between compact and regular environments, a practical solution is to update it in code based on the active trait collection.

swift
1import UIKit
2
3final class TitleViewController: UIViewController {
4    @IBOutlet private weak var titleLabel: UILabel!
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        applyFonts()
9    }
10
11    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
12        super.traitCollectionDidChange(previousTraitCollection)
13        applyFonts()
14    }
15
16    private func applyFonts() {
17        let isRegularWidth = traitCollection.horizontalSizeClass == .regular
18        let size: CGFloat = isRegularWidth ? 28 : 20
19        titleLabel.font = UIFont(name: "AvenirNext-DemiBold", size: size)
20    }
21}

This makes the font decision explicit and keeps it synchronized with the actual runtime size class, not just the Interface Builder preview.

Keep Custom Font Setup Separate from Layout Assumptions

Custom fonts can have very different metrics from San Francisco or the older system fonts that Interface Builder previews were designed around. Ascenders, descenders, and x-height all affect how large the text feels on screen.

That means two fonts set to the same point size may not look equally large. If a custom font appears wrong in one size class, the problem is often visual metrics rather than a broken size-class mechanism.

One helpful pattern is to centralize font selection:

swift
1import UIKit
2
3enum AppFont {
4    static func body(for traits: UITraitCollection) -> UIFont {
5        let size: CGFloat = traits.horizontalSizeClass == .regular ? 18 : 15
6        return UIFont(name: "AvenirNext-Regular", size: size)!
7    }
8}

Then each view asks for the correct font instead of embedding point-size decisions everywhere.

Storyboards Can Help, but Verify at Runtime

You can still use Interface Builder for baseline font selection, but do not assume the storyboard preview proves the runtime result is correct. Custom fonts must also be:

  • included in the app bundle
  • listed in Info.plist
  • loaded by their actual PostScript name

If any of those pieces are wrong, the app may silently fall back to another font and make the size-class behavior look inconsistent when the real issue is font loading.

Common Pitfalls

  • Expecting size classes alone to scale custom fonts automatically the way layouts adapt.
  • Using identical point sizes across devices even when the custom font's metrics differ greatly from the system font.
  • Trusting Interface Builder previews without checking the actual runtime font that loaded.
  • Setting custom fonts in many places instead of centralizing the logic by trait collection or text role.

Summary

  • Size classes are primarily a layout adaptation feature, not a full custom-font scaling system.
  • Custom fonts often need explicit size adjustment in code based on the active trait collection.
  • Two fonts with the same point size can look very different because their metrics differ.
  • Verify that the intended custom font is actually loaded at runtime.
  • Centralized font helpers make size-class-specific custom font behavior much easier to maintain.

Course illustration
Course illustration

All Rights Reserved.