The dilemma of choosing between UILabel and UITextView starts when you want to display text in your Apple iOS, iPadOS or macOS Catalyst App.
I have even gone through this process of choosing UILabel or UITextView to display text on my app.
So I have made a small numbered list to help you out to choose the right option…
Choose UILable to display text:
- If the text word count is small in number
- If Top Aligning text to the top border is not the requirement
- UILable is not tappable or interactive or selectable to the user’s input
- if you want to automatically adjust the font size and fit text to the UILabel bounding box layout
Here is Sample Code for UILabel:
self.helpUILabelText.textAlignment = .left self.helpUILabelText.font = UIFont.systemFont(ofSize: 15, weight: .regular) self.helpUILabelText.numberOfLines = 0 self.helpUILabelText.text = "Your Text" self.helpUILabelText.textColor = .black self.helpUILabelText.lineBreakMode = .byTruncatingTail self.helpUILabelText.isHidden = false
Choose UITextView to display text:
- If the text has a big word count
- If you want top text alignment by default
- If you want text view to be scrollable / You can mark this feature false also
- If you want it interactive with taps from user / You can mark this feature false also
- If you want it editable by the user / You can mark this feature false also
- If you want it to selectable by user / You can mark this feature false also
- If you want line break or paragraph text automatically
Here is Sample Code for UITextView
self.helpUITextView.textAlignment = .left self.helpUITextView.font = UIFont.systemFont(ofSize: 15, weight: .regular) self.helpUITextView.text = "Your Text" self.helpUITextView.textColor = .black self.helpUITextView.sizeToFit() self.helpUITextView.backgroundColor = .clear self.helpUITextView.isHidden = false self.helpUITextView.isEditable = false self.helpUITextView.isScrollEnabled = true self.helpUITextView.isSelectable = false
I hope this information helps you to select the right ‘view’ to display text.
Thanks & Regards
Mandar Apte