Ang Ang - 4 months ago 9
Swift Question

I need to have a UiLabel displaying two integers "minutes:seconds" without shifting left and right as the numbers change

I have created a countdown timer "Minutes:Seconds" the logic works but I have a display issue. As the numbers change the uilabel text shifts ever so slightly left and right and it is ugly. Any ideas?

This is how I change the label currently

let strMinutes = String(format:" %02d",minutes)
let strseconds = String(format:"%02d",displayseconds)

displayTimeLabel.text = "\(strMinutes):\(strseconds) "


To prevent your digits from shifting, use a font with mono-spaced digits such as Courier New Bold, Helvetica Neue, Verdana, American Typewriter, Chalkboard SE, Copperplate, Gill Sans, Marker Felt, or Trebuchet MS.

You set this in the Attributes Inspector for the label:

enter image description here

I would suggest setting the Alignment to center your text as well.

You can do your code in one line:

displayTimeLabel.text = String(format: " %02d:%02d ", minutes, displayseconds)