Mastering UILabel: Displaying Text in Your iOS Applications
UILabel is a fundamental UIKit control for displaying static or dynamic text on the screen. Understanding its capabilities, from basic text display to advanced attributed strings, is crucial for any iOS developer. This article provides a comprehensive guide to mastering UILabel.

Introduction to UILabel
UILabel is a standard UIKit component that you use to display read-only text. Unlike UITextField or UITextView, users cannot edit the content of a UILabel. It's perfect for displaying titles, descriptions, status messages, and other pieces of information that your app presents to the user. You can customize a UILabel's appearance extensively, controlling its font, color, alignment, and even the number of lines it uses to display text.
UILabel is available on iOS (versions 2.0+), iPadOS, tvOS (versions 9.0+), and Mac Catalyst (versions 13.0+).
Let's start by creating a basic UILabel programmatically and adding it to a view hierarchy.
Customizing Label Appearance: Fonts, Colors, and Alignment
Customizing the visual appearance of your UILabel is straightforward and allows you to match your app's design language. You have control over several key properties:
font: Determines the typeface, style, and size of the text. You can use system fonts, custom fonts, or preferred content size fonts for dynamic type.textColor: Sets the color of the text.textAlignment: Specifies how the text is horizontally aligned within the label's bounds (left, center, right, justified, natural).numberOfLines: Controls how many lines the label can display. A value of0means the label will use as many lines as needed to display all the text, given its width constraints.lineBreakMode: Dictates how text is trimmed ifnumberOfLinesis exceeded or if the text doesn't fit within the label's width. Common options include.byWordWrapping,.byTruncatingTail,.byCharWrapping.
Let's explore some of these properties in action:
Working with Attributed Strings for Rich Text
While setting basic text and font properties covers many use cases, sometimes you need to apply different styles to different parts of the same text string. This is where NSAttributedString comes into play. An attributed string allows you to apply a variety of attributes—like different fonts, colors, baseline offsets, and even clickable links—to specific ranges of characters within a single string.
To use an attributed string with a UILabel, you set its attributedText property instead of text. You'll typically create an NSMutableAttributedString to build up your styled text programmatically.
This functionality is available on iOS (versions 3.2+).
Accessibility and UILabel
Ensuring your UILabels are accessible is vital for a good user experience, especially for users relying on VoiceOver or other assistive technologies. By default, UILabel is accessible, and VoiceOver will read its text property.
However, there are scenarios where you might need to provide additional context or override the default behavior. For instance, if your label contains an icon and text, you might want VoiceOver to only read the textual description. Or, if the label's text is not self-explanatory, you can provide a more descriptive accessibility label.
isAccessibilityElement: ForUILabel, this is typicallytrueby default when it has text. You usually don't need to change it.accessibilityLabel: Provides a concise, localized string that identifies the accessibility element to users. If set, this overrides thetextproperty for VoiceOver.accessibilityHint: Provides a brief, localized string that describes the results of performing an action on the accessibility element.
Always test your app with VoiceOver enabled (Settings > Accessibility > VoiceOver) to ensure your labels are being read out correctly and provide meaningful information.
Essential UILabel Properties and Methods
UILabel offers a rich set of properties and methods to give you fine-grained control over your text display. Here's a brief overview of some commonly used ones:
text(String?): The plain text displayed by the label.attributedText(NSAttributedString?): The rich text displayed by the label, allowing for multiple styles within one string.font(UIFont): The font used for the text. Automatically applied to the entire text unlessattributedTextis used.textColor(UIColor): The color of the text. Automatically applied to the entire text unlessattributedTextis used.textAlignment(NSTextAlignment): The horizontal alignment of the text.numberOfLines(Int): The maximum number of lines for displaying the label's text. Set to0for an unlimited number of lines.lineBreakMode(NSLineBreakMode): The technique for breaking and truncating lines of text.adjustsFontSizeToFitWidth(Bool): A Boolean value that determines whether the label reduces the text's font size to fit the label's bounding rectangle.minimumScaleFactor(CGFloat): The minimum acceptable font scale factor to use whenadjustsFontSizeToFitWidthistrue. (iOS 6.0+)
When using Auto Layout, remember to set translatesAutoresizingMaskIntoConstraints = false and define appropriate constraints for your label. Also, for multi-line labels, ensure you provide horizontal constraints to define its width so numberOfLines = 0 can properly estimate the height.
Common Interview Questions
When should I use UILabel versus UITextView or UITextField?
You should use `UILabel` for displaying static, read-only text that users cannot interact with or edit. Use `UITextField` for single-line text input (e.g., usernames, passwords), and `UITextView` for multi-line, scrollable, and potentially editable text (e.g., notes, comments). `UILabel` is lightweight and optimized for display, while `UITextField` and `UITextView` are designed for user input and interaction.
How do I make a UILabel automatically adjust its height for multi-line text?
To make a `UILabel` automatically adjust its height, you need to set its `numberOfLines` property to `0`. Additionally, when using Auto Layout, ensure you provide horizontal constraints (e.g., leading and trailing anchors) to define the label's width. The label will then automatically calculate its required height based on its content, font, and available width.
Can I add tap gestures to a UILabel?
While `UILabel` itself doesn't directly support tap actions (it's not a `UIControl`), you can certainly add a `UITapGestureRecognizer` to a `UILabel` to make it respond to taps. Just make sure to also set the label's `isUserInteractionEnabled` property to `true` to allow it to receive touch events, as it's `false` by default for `UILabel`.