Description
I want to format multiple strings so that they are flush with each other. (See actual result and expected result)
what i tried
I have implemented this solution: https://stackoverflow.com/a/31613297/11582550 (See code) It also works, but only if I print the result in the console. I want to save the text in label.text, that doesn't work.
some code
func formattedString(left:String, right:String) -> String {
let left = (left as NSString).utf8String
let right = (right as NSString).utf8String
print(String(format:"%-20s %-20s", left!, right!))
return String(format:"%-20s %-20s", left!, right!)
}
label.text += formattedString(left: "Firstname: ", right: "Alfred") + "\n" + formattedString(left:"Lastname: ", right: "LongLastname") + "\n" + formattedString(left:"Note:", right: "private")
// actual result
what i expected
## actual result (saved in label.text)
Firstname: Alfred
Lastname: LongLastname
Note: private
## expected result (saved in label.text)
Firstname: Alfred
Lastname: LongLastname
Note: private