Attributed string with custom fonts in storyboard does not load correctly
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Custom fonts set on attributed strings in Interface Builder (Storyboard) often revert to the system font at runtime. This happens because Xcode serializes attributed strings with a font descriptor that may not match the installed custom font at load time. The fix involves setting the attributed string in code rather than in the Storyboard, or ensuring the font is properly registered in Info.plist with the correct PostScript name. This is a long-standing UIKit behavior, not a bug in your code.
The Problem
Interface Builder serializes attributed strings into the Storyboard XML. When the view loads, UIKit deserializes the attributed string and attempts to resolve the font. If the font name in the serialized data does not exactly match the installed font's PostScript name, UIKit falls back to the system font silently.
Fix 1: Set Attributed String in Code
Setting the attributed string in code is the most reliable fix. It bypasses the Storyboard serialization/deserialization issue entirely.
Fix 2: Register Custom Fonts Correctly
The font file must be:
- Added to the Xcode project (drag into the project navigator)
- Included in the target's "Copy Bundle Resources" build phase
- Listed in
Info.plistunderUIAppFonts(key: "Fonts provided by application")
Fix 3: Use the Correct PostScript Name
The font name used in code and Storyboard must be the PostScript name, not the file name. Open the font in Font Book (macOS) and check the "PostScript Name" field.
Fix 4: NSAttributedString with Multiple Styles
Provide a fallback font with ?? in case the custom font fails to load. This prevents invisible text.
Fix 5: IBDesignable Preview
An @IBDesignable subclass applies the custom font both at runtime and in Interface Builder, giving you a visual preview without the serialization bug.
SwiftUI Alternative
SwiftUI's Font.custom() resolves fonts at runtime without Storyboard serialization issues.
Common Pitfalls
- Font file not in Copy Bundle Resources: Adding the font file to the project is not enough. Verify it appears in Build Phases > Copy Bundle Resources. Missing entries cause
UIFont(name:size:)to return nil. - Wrong font name in Info.plist: The
UIAppFontsarray must list the exact file names (e.g., "MyFont-Bold.ttf"), not the PostScript names. One is for registration, the other is for usage in code. - Storyboard attributed strings silently falling back: When the font fails to load, UIKit substitutes the system font without logging a warning. Always test custom fonts on a real device, not just in the Storyboard preview.
- Font caching in Simulator: The iOS Simulator caches fonts aggressively. After adding a new font, clean the build folder (Cmd+Shift+K) and delete the app from the Simulator to force a fresh install.
- Using Display Name instead of PostScript Name: Font Book shows both "Display Name" (e.g., "Avenir Heavy") and "PostScript Name" (e.g., "Avenir-Heavy"). UIKit requires the PostScript name with exact casing.
Summary
- Custom fonts in Storyboard attributed strings often revert to the system font at runtime
- Set attributed strings in code with
UIFont(name:size:)for the most reliable approach - Register fonts in Info.plist (
UIAppFonts) and include them in Copy Bundle Resources - Use the PostScript name (not file name or display name) when referencing fonts in code
- Print all available fonts at runtime to verify your custom font is loaded correctly
- Always provide a fallback font with
??to prevent invisible text if the custom font fails
Related reading
- Auto-fit TextView for Android
- Auto-layout What creates constraints named UIView-Encapsulated-Layout-Width Height?
- Auto Layout constraint on CALayer IOS
- Auto Layout still required after executing -layoutSubviews with UITableViewCell subclass
- AttributeError __enter__ from with tf.Session as sess
- AttributeError 'Adam' object has no attribute 'build' during unpickling
- Auto Scale TextView Text to Fit within Bounds
- Auto Scale TextView Text to Fit within Bounds
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.