Text Label vs Text Field vs Text View (2024)

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):

Text Label vs Text Field vs Text View (1)

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 to 0 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):

Text Label vs Text Field vs Text View (2)

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:

Text Label vs Text Field vs Text View (3)

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 with resignFirstResponder().

  • 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):

Text Label vs Text Field vs Text View (4)

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:

Text Label vs Text Field vs Text View (5)

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:

Text Label vs Text Field vs Text View (6)

Note: A text view is selectable and editable by default. Change the defaults in Interface Builder or in code:

Text Label vs Text Field vs Text View (7)

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:

Text Label vs Text Field vs Text View (2024)
Top Articles
What Is Thematic Investing? | The Motley Fool
How to Block Spam Calls on Your iPhone, Android, or Landline
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
Non Sequitur
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Sen. Emmett Berge

Last Updated:

Views: 5922

Rating: 5 / 5 (80 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Emmett Berge

Birthday: 1993-06-17

Address: 787 Elvis Divide, Port Brice, OH 24507-6802

Phone: +9779049645255

Job: Senior Healthcare Specialist

Hobby: Cycling, Model building, Kitesurfing, Origami, Lapidary, Dance, Basketball

Introduction: My name is Sen. Emmett Berge, I am a funny, vast, charming, courageous, enthusiastic, jolly, famous person who loves writing and wants to share my knowledge and understanding with you.