且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串末尾删除空格字符-UILabel Swift

更新时间:2023-02-21 19:30:11

向标签添加约束似乎对我来说是更好的解决方案.它允许您定义标签和标签之间的明确定义的距离.表格视图单元格的边距.空格的宽度取决于字体,并且如果标签中的文本缩小,甚至会发生变化,从而导致标签中的文本未对齐.表格视图.

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.

话虽如此,您可以防止尾随空格被通过添加零宽度非JOINER"字符(U + 200C)删除:

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.

更新:在iOS 10中,此技巧"似乎不再起作用,因此应按照最初的建议使用布局约束.

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.