1

I have an array of strings, which i need to convert into single String with multi line

 var array = ["A","B","C","D","E"]
 var multiLineString = //convert array to a string
 println("\(multiLineString)")

The output should be:

  A 
  B 
  C 
  D 
  E 
1

3 Answers 3

3

That should be something like:

var array = ["A","B","C","D","E"]
var multiLineString = join("\n", array)
println("\(multiLineString)")

Note that the console does not print this on multiple lines.

UPDATE: To get the height of the label to display this string:

let label = UILabel()
label.text = multiLineString
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.sizeToFit()
println("Height: \(label.frame.height)")
Sign up to request clarification or add additional context in comments.

2 Comments

i need to get the total height of a string. so that i can display string in multi lines in a cell. Does 'multiLineString' gives the correct height?
@iPhoneGuy see my updated answer how to get the correct height of a label displaying this string.
1

Try join:

var array = ["A","B","C","D","E"]
var multiLineString = join("\n", array)
println("\(multiLineString)")

3 Comments

i need to get the total height of a string. so that i can display string in multi lines in a cell. Does 'multiLineString' gives the correct height?
@iPhoneGuy: You seem to mix different problems here. A string is just a collection of characters, unrelated to the visual represention. The height depends on other parameters, such as the selected font. Perhaps this stackoverflow.com/questions/7174007/… (and the related questions) is what you are looking for.
Correct @MartinR. However, I updated my answer above to display how you can get the height of a label displaying the string.
1

Since Swift 2.0 this would produce the error:

Cannot invoke join with an argument list of type (String, [String])

Use this instead:

array.joinWithSeparator("\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.