How do I change the font size of a UILabel in Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
UILabels in iOS development serve as a versatile text-display component, enabling developers to present information to users with a variety of styling options. One commonly adjusted attribute is font size, essential for adhering to design specifications and enhancing readability. This article explains how to change the font size of a UILabel in Swift.
Changing Font Size of UILabel in Swift
Basic Approach: Using System Fonts
Swift provides an easy method for adjusting the font size of a UILabel using system fonts. System fonts are pre-defined by Apple and adapt well to different text styles and sizes.
In this example, UIFont.systemFont(ofSize:) adjusts the font size implicitly while keeping the default system font face. ofSize: 20 specifies the desired font size, which you can modify to fit your requirements.
Custom Fonts
If you want greater typographic control, you can utilize custom fonts. Adding a custom font to an Xcode project involves incorporating the font file and updating the Info.plist.
- Import the Font: Drag the font file (e.g.,
CustomFont.ttf) into your Xcode project. - Update
Info.plist: UnderFonts provided by application, add the font file name.
Here, UIFont(name:size:) applies the custom font and changes the size to 24 points.
Dynamic Type and Accessibility
Adopting Dynamic Type allows your app to respond gracefully to user-specified text size changes, ensuring accessibility. Using a predefined UIFont.TextStyle, the text adjusts automatically in accordance with system settings.
In this scenario, .body is a text style that can be adjusted according to user settings. Setting adjustsFontForContentSizeCategory to true ensures that any changes in accessibility settings of the system are respected.
Using Auto Layout
When using Auto Layout, it's important for UILabels to resize gracefully with the container view. Ensure that constraints do not conflict with text resizing, potentially cutting off content or causing layout issues.
Combining Styles
You can combine typefaces and sizes for a more sophisticated appearance using NSAttributedString. This allows specific parts of the text to adopt distinct styles.
The above example changes "Hello" to system font size 16 and "Swift!" to a bold font size 24.
Summary Table
Below is a table summarizing the key methods to change a UILabel's font size:
| Method | Description |
systemFont(ofSize:) | Uses the default system font with a specified size. |
UIFont(name:size:) | Utilizes a custom font file and defines its size. |
preferredFont(forTextStyle:) | Supports Dynamic Type for accessibility. |
NSAttributedString with .font | Applies multiple styles to a single UILabel text. |
Additional Considerations
- Performance: Overusing custom fonts with extensive text can impact performance. Cache font instances when used repeatedly.
- Localization: Be mindful of how different languages may influence text length and presentation. Auto Layout can alleviate some discrepancies by offering flexible constraints.
- Testing: Ensure proper testing on various device sizes and configurations to confirm text displays correctly.
By understanding these key methods for adjusting UILabel font sizes, you can produce text interfaces that meet design requirements and enhance user experiences efficiently. Whether using system fonts, custom fonts, or leveraging Dynamic Type, Swift provides extensive tools to manipulate typography effectively.

