How do you show multiple lines of text in a UILabel using Swift? [duplicate]
Asked Answered
I

3

5

Is it possible to show multiple lines of text in a UILabel in Swift?

I currently use a UITextView which has an internal scroll and I would like to avoid a scroll and display statically. I’ve tried placing a number of constraints to achieve this but it doesn’t prevent an internal scroll on the UITextView.

Inhesion answered 12/4, 2018 at 8:13 Comment(2)
set label.numberOfLines = 0 to have unlimited amount of rows and textField.isScrollingEnabled = false to inactivate the scroll in the text fieldMarco
Possibly related: #24135405Disjoined
B
7

I think you mean UITextView.

Change your text to a UILabel and hook it up as an IBOutlet and then add the following code:

myLabel.numberOfLines = 0

Example:

@IBOutlet weak var myLabel: UILabel!

    override func viewDidLoad() {
    super.viewDidLoad()

    myLabel.text = "This is where all of your text goes..."
    myLabel.numberOfLines = 0

}

Set your constraints as normal and this should display the UILabel on multiple lines dependent on how much text you have displayed.

Brutish answered 12/4, 2018 at 8:28 Comment(1)
Thank you this makes much more senseInhesion
C
2

You can simply do the following :

myLabel.numberOfLine = 2

You can also specify 0 to allow your label to use the necessary number of line to display all the content.

Chantal answered 12/4, 2018 at 8:16 Comment(0)
D
1

You can do that Using StoryBoard also:

Make the lines to 0 or any amount of lines you want

enter image description here

Dierdre answered 12/4, 2018 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.