If you’re new to iOS development the differences between a text label, a text field or a text view can be confusing. Here’s a quick guide for when you’re not sure which to use.
Text Label
When you want to show some text, but don’t need the user to enter or edit the text your first choice should be a plain text label (UILabel
):
Options available in both Interface Builder and programmatically allow you to change properties such as the font, text alignment, and text color.
let label = UILabel()label.text = "Hello World!"label.font = UIFont.preferredFont(forTextStyle: .body)label.adjustsFontForContentSizeCategory = truelabel.textColor = .redlabel.backgroundColor = .yellowlabel.numberOfLines = 0
Notes:
- For a multi-line label that uses as many lines as needed set
numberOfLines
to0
and constrain the width of the label. - If using dynamic type (> iOS 10) set the property to have the label automatically update the font when the user changes their preferred text size.
Text Field
When you want the user to enter a single line of text use a text field (UITextField
):
let textField = UITextField()textField.placeholder = "Enter your password"textField.delegate = self // UITextFieldDelegate
Like a text label, you can set the font, text color, alignment, and other properties. A text field also supports the UITextInputTraits
protocol which allows you to control the keyboard type and appearance, auto-capitalization and correction, the return key, spell-checking and secure-text entry:
textField.textContentType = .passwordtextField.isSecureTextEntry = true
Notes:
See the various methods of the
UITextFieldDelegate
to interact with the text field as the user enters text.The on-screen keyboard is shown when the text field becomes the first responder (usually when the user taps on the field). You can force a text field to become the first responder by calling
becomeFirstResponder()
and remove it withresignFirstResponder()
.To stop the on-screen keyboard from covering content you probably want to manage the keyboard.
Text View
When you want to the user to enter more than one line of text use a text view (UITextView
):
let textView = UITextView()textView.text = "Hello World"
A text view supports UITextInputTraits
like a text field, but the text in a text view can also be editable, scrollable or selectable. A text view is scrollable by default. You can disable scrolling in the scroll view section of the attributes inspector in Interface Builder:
Or if you prefer in code:
textView.isScrollEnabled = false
Note: A text view does not have an intrinsic content size when scrolling is enabled.
Even if you do not want a text view to be editable or scrollable you might still use it to display text instead of a text label to allow the user to select, copy and lookup text:
Note: A text view is selectable and editable by default. Change the defaults in Interface Builder or in code:
textView.isSelectable = falsetextView.isEditable = false
Summary
UILabel
: For displaying one or more lines of text. Selection, editing, and scrolling are not possible.UITextField
: For entering a single line of text. Use secure entry for sensitive data such as passwords.UITextView
: For displaying or entering one or more lines of text. Selection, editing, and scrolling are possible.
Read More
See the following WWDC session for a flowchart to help you choose the right text control:
If you want to find out more about using text labels, text fields, and text views to build adaptive layouts see my book on Auto Layout: