Hacker4Lifes
Hacker4Lifes

Reputation: 51

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

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

Upvotes: 5

Views: 20799

Answers (2)

TimLR
TimLR

Reputation: 267

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

Upvotes: 3

Peyman
Peyman

Reputation: 3077

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.

Upvotes: 15

Related Questions