Dynamically changing font size of UILabel
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UILabel can automatically shrink its font to fit text within its bounds using adjustsFontSizeToFitWidth and minimumScaleFactor. For programmatic control, set the font property directly. For system-wide Dynamic Type support (accessibility), use UIFont.preferredFont(forTextStyle:) with adjustsFontForContentSizeCategory = true. Each approach serves a different use case — auto-shrinking for layout constraints, programmatic changes for state-driven UI, and Dynamic Type for accessibility.
Auto-Shrinking to Fit Width
minimumScaleFactor is a ratio of the original font size. A value of 0.5 with a 24pt font allows shrinking down to 12pt. The label shrinks only when the text does not fit at the original size.
Setting Font Size Programmatically
Dynamic Type (Accessibility)
When the user changes the system font size in Settings > Accessibility > Display & Text Size, labels with adjustsFontForContentSizeCategory = true update automatically.
Custom Fonts with Dynamic Type
UIFontMetrics scales your custom font proportionally to the user's Dynamic Type setting.
Auto Layout Considerations
Scaling Based on Screen Size
Attributed Text with Multiple Sizes
Interface Builder Configuration
In Storyboard or XIB:
- Select the UILabel
- In the Attributes inspector:
- Font: Choose the font and size
- Autoshrink: Set to "Minimum Font Scale" and enter a value (e.g., 0.5)
- Lines: Set to 1 for auto-shrinking, 0 for multi-line wrapping
- For Dynamic Type: set Font to a text style (Body, Title, etc.) and check "Automatically Adjusts Font"
Responding to Content Size Changes
Common Pitfalls
adjustsFontSizeToFitWidthrequiresnumberOfLines = 1: Auto-shrinking only works for single-line labels. For multi-line labels, usenumberOfLines = 0with auto layout constraints and let the label grow vertically instead.minimumScaleFactor = 0: A scale factor of 0 means the text can shrink to nearly invisible. Set a reasonable minimum like 0.5 or 0.7 to keep text readable.- Ignoring Dynamic Type: Apps that do not support Dynamic Type fail accessibility reviews. Always use
preferredFont(forTextStyle:)for user-facing text. - Overriding font in
layoutSubviews: Settinglabel.fontinlayoutSubviewscauses an infinite loop because changing the font triggers a layout pass. Set fonts inviewDidLoador when data changes, not in layout methods. - Mixing
textandattributedText: SettingattributedTextoverridestextand vice versa. If you set an attributed string, subsequentlabel.font = ...changes are ignored — modify the attributed string's attributes instead.
Summary
- Use
adjustsFontSizeToFitWidth = truewithminimumScaleFactorfor auto-shrinking single-line labels - Set
label.font = UIFont.systemFont(ofSize:)for programmatic size changes - Use
UIFont.preferredFont(forTextStyle:)withadjustsFontForContentSizeCategory = truefor Dynamic Type accessibility - Scale custom fonts with
UIFontMetrics(forTextStyle:).scaledFont(for:) - For multi-line labels, use
numberOfLines = 0with auto layout instead of auto-shrinking
Related reading
- Dynamically hiding view in SwiftUI
- Easiest way to detect Internet connection on iOS?
- Easiest way to post to a server on iPhone? I don't care about the response
- Easy way to see saved NSUserDefaults?
- Eclipse hangs at the Android SDK Content Loader
- edit-config for ios usage descriptions doc.find is not a function
- Editing screenshots in iTunes Connect after iOS app was approved
- ektorp couchDB to android replication
.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.