Sergey Di Sergey Di - 2 months ago 23
iOS Question

UILabel alignment right issue

I have a problem with UILabel in Swift. Problem happens when text is changed often (like a time readout) in the label.

With alignment left everything works perfect. But I need alignment right.

And in this mode text jumps left and right when changed. This jump is about 1 pixel.

May be someone know how to solve this problem?

n_b n_b

Based on the code in your comment:

label.font = label.font.withSize(12)

You are using the standard iOS system font, which is not monospaced, and therefore will cause spacing issues when the text changes. If you use a monospaced font (Courier, Menlo, etc), this issue will go away.

Alternatively, you could handle the drawing of the text yourself or adjust the font's kerning.