Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space characters being removed from end of String - UILabel Swift

Tags:

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)  " + " " 
like image 837
Greg Peckory Avatar asked Jul 21 '15 09:07

Greg Peckory


1 Answers

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.

like image 168
Martin R Avatar answered Sep 20 '22 19:09

Martin R