0

I am trying to take each string in my array and list them in a label one string per line. I tried using the joined method with /n to attempt to make it got to the next line but it just literally puts /n in between each string. I'm sorry if this happens to be a duplicate but unless I'm wording my question wrong I cant seem to find an answer. This is an example of what I'm looking for.

String[0] 
String[1] 
String[2]
and so on...

2 Answers 2

3

Try this:

let array = ["The", "quick", "brown", "fox"]    
let string = array.joined(separator: "\n")

joined returns a new string by concatenating the elements of the sequence, adding the given separator (in this case, a line break) between each element in the array.

That will return this:

The
quick
brown
fox

...and set yourLabel.numberOfLines = 0

From Apple's documentation:

The default value for this numberOfLines is 1. To remove any maximum limit, and use as many lines as needed, set the value of numberOfLines to 0.

Sign up to request clarification or add additional context in comments.

Comments

1

First make sure that the label can display multiple lines. If the UILabel is named lblText, then:

lblText.numberOfLines = 0

Then, simply use string interpolation to add in the line feeds:

lblText.text = "\(String[0])\n\(String[1])\n\(Stribng[2])"

The issue might be that you used "/n" instead of "\n" :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.