Setting UILabel text to bold
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To set a UILabel's text to bold in iOS, assign a bold UIFont to its font property. The most common approach is UIFont.boldSystemFont(ofSize:) for the system bold font, or UIFont.systemFont(ofSize:weight:) for specific weights like .semibold or .heavy. For dynamic type support, use UIFont.preferredFont(forTextStyle:) with a bold text style. For partial bold text (bolding only certain words), use NSAttributedString with a bold font attribute applied to specific ranges.
System Bold Font
Available Font Weights
Dynamic Type (Accessibility-Friendly)
Custom Font with Dynamic Type Scaling
Custom Bold Fonts
Bold Variant of Any Font
Partial Bold with NSAttributedString
Bold only specific words in a label:
Helper for Bold Substrings
Interface Builder / Storyboard
To set bold in Interface Builder:
- Select the UILabel in the storyboard
- Open the Attributes Inspector
- Click the Font field (the "T" icon)
- Change Style from "Regular" to "Bold"
- Or select a specific weight like "Semibold" or "Heavy"
SwiftUI Equivalent
Common Pitfalls
- Using
UIFont(name:size:)with a wrong font name: If the font name is incorrect (e.g.,"Helvetica Bold"instead of"Helvetica-Bold"), the initializer returnsniland the label falls back to the default font. PrintUIFont.fontNames(forFamilyName:)to find the exact name. - Not enabling
adjustsFontForContentSizeCategory: UsingpreferredFont(forTextStyle:)without settingadjustsFontForContentSizeCategory = truemeans the font size is set once and never updates when the user changes their text size preference in Settings. - Applying
attributedTextafter settingtext: Settinglabel.textclearsattributedText, and vice versa. If you setattributedTextand then settext, the attributed formatting is lost. Use one or the other, not both. - Forgetting
size: 0inUIFont(descriptor:size:): When creating a font from a descriptor (e.g., adding bold trait), passingsize: 0preserves the original font size. Passing a specific size overrides it, which may cause unexpected size changes. - Hardcoding font sizes instead of using Dynamic Type: Hardcoded sizes like
UIFont.boldSystemFont(ofSize: 17)do not respond to the user's accessibility text size settings. UseUIFontMetrics.scaledFont(for:)to make custom bold fonts support Dynamic Type.
Summary
- Use
UIFont.boldSystemFont(ofSize:)or.systemFont(ofSize:weight: .bold)for bold system fonts - Use
UIFont.preferredFont(forTextStyle: .headline)withadjustsFontForContentSizeCategory = truefor Dynamic Type support - Use
fontDescriptor.withSymbolicTraits(.traitBold)to get the bold variant of any font - Use
NSAttributedStringwith.fontattribute to bold specific words within a label - In SwiftUI, use
.bold()or.fontWeight(.bold)modifiers
Related reading
- Setting up buttons in SKScene
- SHA256 in swift
- Shadow Effect for a Text in Android?
- Shall we always use unowned self inside closure in Swift
- Share data between two or more iPhone applications
- Shared preferences for creating one time activity
- SharedPreferences.onSharedPreferenceChangeListener not being called consistently
- Sharing link on WhatsApp from mobile website not application for 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.