I have the following code for a Table View Cell in Swift
let rcap = cell.viewWithTag(613) as! UILabel rcap.text = "Capacity: \(room.capacity) " // I added space at the end
The space characters at the end of the string, are removed when shown on screen.
If I add space characters at the beginning of the string there is no issue.
At the moment I am using this 'full stop' hack, but it is not good enough:
rcap.text = "Capacity: \(room.capacity) ."
Any ideas?
I also tried:
rcap.text = "Capacity: \(room.capacity) " + " "
Adding a constraint to the label seems like the better solution to me. It allows you to define a well-defined distance between the label and the margin of the table view cell. The width of a space is dependent on the font and might even change if the text in the label is shrunk, causing non-aligned texts in the table view.
Having said that, you can prevent the trailing space from being removed by appending a "ZERO WIDTH NON-JOINER" character (U+200C):
rcap.text = "Capacity: \(room.capacity) \u{200c}"
But I consider that more as a "trick" than the proper solution to the problem.
Update: It seems that this "trick" does not work any more in iOS 10, so a layout constraint should be used instead, as initially suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With