将字符串数组显示为多行字符串 - Swift

时间:2014-10-27 09:08:12

标签: ios swift string

我有一个字符串数组,我需要将其转换为带有多行的单个字符串

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

输出应为:

  A 
  B 
  C 
  D 
  E 

3 个答案:

答案 0 :(得分:3)

那应该是这样的:

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

请注意,控制台不会在多行上打印它。

更新: 要获得标签的高度以显示此字符串:

let label = UILabel()
label.text = multiLineString
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.sizeToFit()
println("Height: \(label.frame.height)")

答案 1 :(得分:1)

尝试join

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

答案 2 :(得分:1)

由于Swift 2.0,这会产生错误:

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

请改用:

array.joinWithSeparator("\n")