Custom font size for Text in SwiftUI
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In SwiftUI, customize font size for Text views using .font(.system(size:)) for system fonts, .font(.custom("FontName", size:)) for custom fonts, or .font(.title) for semantic text styles that scale with Dynamic Type. SwiftUI's built-in text styles (.body, .title, .caption, etc.) automatically support accessibility scaling, while fixed-size fonts require explicit opt-in via @ScaledMetric or .dynamicTypeSize().
System Fonts with Custom Size
Available weights: .ultraLight, .thin, .light, .regular, .medium, .semibold, .bold, .heavy, .black.
Available designs: .default, .rounded, .monospaced, .serif.
Semantic Text Styles (Recommended)
SwiftUI provides text styles that scale automatically with the user's Dynamic Type setting:
These styles are the best choice for most UI text because they respect the user's accessibility preferences.
Custom Fonts
Load a custom font file (.ttf or .otf) included in your app bundle:
Registering Custom Fonts in Info.plist
Dynamic Type Scaling
@ScaledMetric for Custom Sizes
@ScaledMetric adjusts the value proportionally when the user changes their preferred text size in Settings.
Limiting Dynamic Type Range
Text Modifiers
Combine font size with other text styling:
Font Weight Without Changing Size
Conditional Font Sizes
Custom Font Extension
Create a reusable font system:
Common Pitfalls
- Using fixed font sizes instead of semantic text styles:
.font(.system(size: 17))does not scale with Dynamic Type. Users with accessibility needs see the same small text regardless of their settings. Use.font(.body)or.custom("Font", size: 17, relativeTo: .body)for accessible text. - Wrong custom font name string: The font name passed to
.custom()must be the PostScript name, not the file name. UseUIFont.familyNamesandUIFont.fontNames(forFamilyName:)to find the correct name. A wrong name silently falls back to the system font. - Forgetting to register fonts in Info.plist: Custom font files must be listed under
UIAppFontsinInfo.plistand included in the app target. Without registration,.custom("MyFont", size: 20)silently uses the system font instead. - Setting
.fontWeight()on a custom font:.fontWeight()only works with system fonts. For custom fonts, use the specific font variant name (e.g.,"Avenir-Heavy"instead of"Avenir"with.bold()). - Not testing with large Dynamic Type sizes: Text that looks fine at default size may clip, overlap, or break layouts at the largest accessibility sizes. Always test with
Xcode > Accessibility Inspectoror.environment(\.sizeCategory, .accessibilityExtraExtraExtraLarge).
Summary
- Use
.font(.system(size:weight:design:))for system fonts with specific sizes - Use
.font(.body),.font(.title), etc. for accessible, auto-scaling text - Use
.font(.custom("FontName", size:, relativeTo:))for custom fonts that scale with Dynamic Type - Use
@ScaledMetricto make custom numeric values scale with accessibility settings - Create a
Fontextension for consistent typography across your app

