Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a new line in a swift label.text

Tags:

swift

I am making an app and I want to add a new line, in a Teach.text

func RandomQuestions(){

        var RandomNumber = arc4random() % 1
        RandomNumber += 1

        switch(RandomNumber){
        case 1:
            QuestionLbl.hidden = false
           QuestionLbl.text = "2x + 4 = 14"
            Button1.setTitle("x = 5", forState: UIControlState.Normal)
           Button2.setTitle("x = 4", forState: UIControlState.Normal)
           Button3.setTitle("x = 9", forState: UIControlState.Normal)
            Button4.setTitle("y = 5", forState: UIControlState.Normal)
            Teach.text = "2x + 4 = 14"
            CorrectAnswer = "1"
            break

        default:
            break

        } 

    }

So in other words, I need to add another line, so when I am testing my app, the 'Teach' should say

2x + 4 = 14
2x = 10

like image 216
Hacker4Lifes Avatar asked Nov 25 '15 22:11

Hacker4Lifes


2 Answers

If it's a UILabel, set numberOfLines field to 0

Teach.numberOfLines = 0
Teach.text = "Line1\nLine2"

And make sure it's tall enough to show both lines.

like image 77
Peyman Avatar answered Sep 30 '22 20:09

Peyman


Just add the following code

textLabel.lineBreakMode = .ByWordWrapping
textLabel.numberOfLines = 0 // 0 = unlimited lines, you can also set the in your storyboard

Hope that helps

like image 30
TimLR Avatar answered Sep 30 '22 21:09

TimLR