28

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?

Gustavo Barbosa
  • 1,340
  • 1
  • 17
  • 27
  • 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). – toraritte Apr 06 '18 at 17:00

3 Answers3

41

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.

garrettmurray
  • 3,338
  • 1
  • 25
  • 23
  • Hey Garret! It totally makes sense. Thanks for the answer. – Gustavo Barbosa Aug 06 '13 at 01:56
  • I think your answer is wrong, please have a look at the following one. – Vishnu Kumar. S Jul 27 '15 at 09:46
  • [Text Programming Guide for iOS](https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/UsingTextClasses/UsingTextClasses.html) 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`). – toraritte Apr 06 '18 at 17:05
  • 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. – garrettmurray Apr 17 '18 at 05:26
  • But you can't specify wrap content for text view; how do you make it fit only the words presented? – pete Aug 14 '20 at 03:34
26

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

Vishnu Kumar. S
  • 1,797
  • 1
  • 15
  • 35
7

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.

AADProgramming
  • 6,077
  • 11
  • 38
  • 58