iOS UI best practice - UILabel or UITextView?

21,023

Solution 1

Update: As I commented below, I agree this is not the best answer these days. It was more accurate back in 2013, but today you should use a text view for a large text field. I cannot delete this, as it's the accepted answer, but I agree (and voted for) Vishnu's answer below.

This depends a bit on what exactly you're trying to do.

If you need to display static text with no editing features and no selection features, you should use a UILabel and place it in a UIScrollView if necessary.

If you need selection or editing, use UITextView.

Solution 2

Sorry for the late reply, but this may help someone.

According to Apple's Text Programming Guide for iOS, UILabel defines a label, which displays a static text string. UITextField defines a text field, which displays a single line of editable text. UITextView defines a text view, which displays multiple lines of editable text. Although these classes actually can support the display of arbitrary amounts of text, labels and text fields are intended to be used for relatively small amounts of text, typically a single line. Text views, on the other hand, are meant to display large amounts of text.

For more information please check: Text Programming Guide for iOS

Solution 3

What UI component would you recommend to display the article's body? An UILabel with numberOfLines equals to zero or an UITextView with no scrolling and fixed size?

Let's refer official documentation from Apple, for guidelines for usage of text content in app.

According to it, UIKit framework provides three primary classes for displaying text content in an app’s user interface: UILabel , UITextField and UITextView.

Below line hints at a possible recommendation for a specific use case:

Although these classes actually can support the display of arbitrary amounts of text, labels and text fields are intended to be used for relatively small amounts of text, typically a single line. Text views, on the other hand, are meant to display large amounts of text.

If we consider a use case of displaying an Article, it is fair to assume it will have a substantial text content. And hence if we apply above guidelines, UITextView seems to be a better tool for the job.

Next, when user is reading an article text, UITextView has selectable text. This means that user can copy specific lines from article or get the device to speak the text too.

Share:
21,023

Related videos on Youtube

Gustavo Barbosa
Author by

Gustavo Barbosa

Updated on July 09, 2022

Comments

  • Gustavo Barbosa
    Gustavo Barbosa almost 2 years

    If you are creating an UIView for an iPhone application to display an article from a blog, for example.

    What UI component would you recommend to display the article's body? An UILabel with numberOfLines equals to zero or an UITextView with no scrolling and fixed size?

    Why?

    • toraritte
      toraritte about 6 years
      I agree with @Vishnu Kumar's answer below: I needed to display a "tooltip" for my app and the easiest way was to use UITextView with isEditable set to false (making it static).
  • Gustavo Barbosa
    Gustavo Barbosa almost 11 years
    Hey Garret! It totally makes sense. Thanks for the answer.
  • Vishnu Kumar. S
    Vishnu Kumar. S almost 9 years
    I think your answer is wrong, please have a look at the following one.
  • toraritte
    toraritte about 6 years
    Text Programming Guide for iOS in @Vishnu answer below is the right answer for this question. It even suggests on how to view web content, such as blog posts (quote: Use Web Views to Display Web Content).
  • garrettmurray
    garrettmurray about 6 years
    Agree this is not the best answer these days. It was more accurate back in 2013, but today you should use a text view for a large text field. I cannot delete this, as it's the accepted answer, but I agree (and voted for) Vishnu's answer below.
  • pete
    pete over 3 years
    But you can't specify wrap content for text view; how do you make it fit only the words presented?