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
They’re Spectacular! Explore the Many Homes of Newly Minted Billionaire Jerry Seinfeld
Crypto.com | Securely Buy, Sell & Trade Bitcoin, Ethereum and 350+ Altcoins
Dragon Age Inquisition War Table Operations and Missions Guide
Jordanbush Only Fans
Matgyn
Greedfall Console Commands
Unblocked Games Premium Worlds Hardest Game
Routing Number 041203824
A Fashion Lover's Guide To Copenhagen
Garrick Joker'' Hastings Sentenced
Chastity Brainwash
Sport Clip Hours
Ree Marie Centerfold
Mission Impossible 7 Showtimes Near Regal Bridgeport Village
Dump Trucks in Netherlands for sale - used and new - TrucksNL
Unlv Mid Semester Classes
Samantha Lyne Wikipedia
Pay Boot Barn Credit Card
No Hard Feelings - Stream: Jetzt Film online anschauen
Hyvee Workday
Juicy Deal D-Art
Gran Turismo Showtimes Near Marcus Renaissance Cinema
Academy Sports Meridian Ms
Skycurve Replacement Mat
Craigslist Dubuque Iowa Pets
Annapolis Md Craigslist
Paradise Point Animal Hospital With Veterinarians On-The-Go
Spirited Showtimes Near Marcus Twin Creek Cinema
Why Are The French So Google Feud Answers
The value of R in SI units is _____?
Sun-Tattler from Hollywood, Florida
Scioto Post News
Tenant Vs. Occupant: Is There Really A Difference Between Them?
Honda Ruckus Fuse Box Diagram
Craigs List Jonesboro Ar
Duff Tuff
Rochester Ny Missed Connections
Ktbs Payroll Login
Miracle Shoes Ff6
Gateway Bible Passage Lookup
Lovein Funeral Obits
20 bank M&A deals with the largest target asset volume in 2023
Jack In The Box Menu 2022
Dinar Detectives Cracking the Code of the Iraqi Dinar Market
Pink Runtz Strain, The Ultimate Guide
Darkglass Electronics The Exponent 500 Test
Lebron James Name Soundalikes
Hughie Francis Foley – Marinermath
Who uses the Fandom Wiki anymore?
18 Seriously Good Camping Meals (healthy, easy, minimal prep! )
Electric Toothbrush Feature Crossword
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.