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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Jansenbase
  • 69
  • 1
  • 6
  • 1
    set `label.numberOfLines = 0` to have unlimited amount of rows and `textField.isScrollingEnabled = false` to inactivate the scroll in the text field – Vollan Apr 12 '18 at 08:17
  • 1
    Possibly related: https://stackoverflow.com/questions/24134905/how-do-i-set-adaptive-multiline-uilabel-text – Ahmad F Apr 12 '18 at 08:21

3 Answers3

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.

Super_Simon
  • 1,250
  • 8
  • 25
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.

CZ54
  • 5,488
  • 1
  • 24
  • 39
1

You can do that Using StoryBoard also:

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

enter image description here

Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23