iOS set font size of UILabel Programmatically
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Changing a UILabel font size programmatically is straightforward in UIKit, but the best solution depends on whether you want a fixed size, a weighted system font, a custom font, or dynamic type support. The API for all of these flows through the label’s font property. Good code also considers Auto Layout and accessibility so the label still behaves correctly after the font changes.
Set a Fixed System Font Size
The simplest approach is to assign a system font with the size you want.
This is the direct answer when the design calls for a specific fixed size.
Use Weight and Style Intentionally
Font size is only one part of the visual result. If the label should be bold or semibold, set that at the same time instead of treating size and weight as unrelated tweaks.
This is usually clearer than using the default system weight and then trying to fake emphasis elsewhere with color or spacing.
Load a Custom Font Safely
If the app uses a bundled font, use UIFont(name:size:) and handle failure explicitly. That method returns an optional because the font must be registered correctly in the app bundle.
The fallback matters because a missing font name otherwise leaves the label with no obvious styling fix.
Support Dynamic Type When Text Should Scale
For user-facing content, a fixed point size is not always the best choice. If the label should respect the user’s text-size preferences, use text styles and dynamic type.
This allows the label to scale with accessibility settings instead of remaining locked to one size. It is often the better default for content labels, while fixed sizes remain more appropriate for tightly controlled branding or decorative UI.
Let the Label Resize Correctly
Changing the font can also affect layout. A larger font may need multiple lines or updated constraints.
If the label is clipped after increasing the font size, the problem is often not the font API itself. The real problem is a frame or constraint that no longer fits the text.
When using frames instead of Auto Layout, call sizeToFit() if appropriate:
That updates the label’s frame to match the new rendered text size.
Make Reusable Styling Helpers
If the same font rules appear in multiple screens, wrap them in a helper so the intent stays consistent.
This reduces one-off styling drift and makes future updates easier.
Common Pitfalls
- Setting a larger font size without updating constraints or allowing line wrapping.
- Using a custom font name that is not actually registered in the app bundle.
- Choosing fixed sizes everywhere and ignoring dynamic type for content-heavy screens.
- Calling
UIFont.systemFont(ofSize:)when the design really needs a specific weight or text style. - Changing the font but forgetting that frame-based layouts may need
sizeToFit().
Summary
- Set a
UILabelfont size by assigning a newUIFontto thefontproperty. - Use weighted system fonts when the label needs emphasis as well as size.
- Handle custom fonts safely with a fallback.
- Prefer dynamic type for content that should respect accessibility text settings.
- If the text clips after resizing, fix the layout rather than the font call.

