How do I set bold and italic on UILabel of iPhone/iPad?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To make a UILabel bold or italic, you set its font to a UIFont that carries the desired traits. If the entire label uses one style, a font change is enough. If only part of the text should be bold or italic, use an attributed string instead of a plain string.
Set a Uniform Bold Font
For a label where the whole text should be bold, the simplest solution is a system bold font:
This is the most direct solution when you only need bold styling and do not care about mixing traits in the same label.
Set an Italic Font with a Descriptor
UIKit has a convenience method for bold system fonts, but italic styling is commonly done through a font descriptor.
This asks the system font for an italic variant. If the descriptor cannot produce the requested trait, the fallback should remain a readable non-italic font rather than crashing.
Combine Bold and Italic
If you want the label font to be both bold and italic, combine the symbolic traits:
Whether this produces the exact look you want depends on the font family. Some fonts support the combined face directly, and others approximate it less elegantly.
Use NSAttributedString for Partial Styling
If only one word or range should be bold or italic, assign an attributed string to attributedText.
This is the right approach when different parts of the label need different emphasis.
Custom Fonts Need Actual Available Faces
If you use a custom font family, do not assume that adding symbolic traits to one font face will always produce a valid bold or italic version. Many custom fonts ship separate face names such as:
- '
MyFont-Regular' - '
MyFont-Bold' - '
MyFont-Italic' - '
MyFont-BoldItalic'
In those cases, loading the exact face by name is more reliable than asking the descriptor system to synthesize traits.
Consider Dynamic Type
If the label participates in Dynamic Type, keep accessibility in mind. Styling and sizing are separate concerns. You can use UIFontMetrics to scale a styled font so bold or italic text still responds correctly to the user's text size settings.
Common Pitfalls
- Using
textwhen the requirement really needsattributedText. - Assuming every font supports italic or bold-italic variants.
- Replacing the whole font and accidentally losing Dynamic Type behavior.
- Applying one font to the entire label when only part of the text should be emphasized.
- Expecting symbolic traits to work identically across all custom font families.
Summary
- Use
UIFont.boldSystemFontwhen the whole label should be bold. - Use a font descriptor with
.traitItalicfor italic styling. - Combine traits for bold-italic when the font supports it.
- Use
NSAttributedStringfor mixed styles within one label. - With custom fonts, prefer loading real face names instead of assuming traits can be synthesized.
Related reading
- How do I set bold and italic on UILabel of iPhone/iPad?
- How do I set the height of tableHeaderView UITableView with autolayout?
- How do I set the size of a SF Symbol in SwiftUI?
- How do I set up a simple delegate to communicate between two view controllers?
- How do I set up IntelliJ IDEA for Android applications?
- How do I set up IntelliJ IDEA for Android applications?
- How do I shake an Android device within the Android emulator to bring up the dev menu to debug my React Native app
- How do I show the number keyboard on an EditText in android?
.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.