Custom font size for Text in SwiftUI
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
Related reading
- Custom Font Sizing in Xcode 6 Size Classes not working properly with Custom Fonts
- Custom fonts and XML layouts Android
- Custom fonts in iOS 7
- Custom init for UIViewController in Swift with interface setup in storyboard
- Custom installed font not displayed correctly in UILabel
- custom listview adapter getView method being called multiple times, and in no coherent order
- Custom navigation with Navigator component in React-Native
- Custom UITableViewCell from nib in Swift
.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.