iOS Prevent timer UILabel 'shaking' when numbers change

countdown timer swift 4
timer.scheduledtimer swift 4
run timer in background ios swift
stop timer swift
swift timer not firing
countdown timer swift 4 github
reset timer swift
ios timer start

I have a UILabel which shows the output of a timer in the format MM:ss:SS (minutes, seconds, centiseconds), however it "shakes" from left to right as the width of the centiseconds changes - "11" is narrower than "33" for example.

Is there any way I can mitigate this? I've tried centring it, giving it a fixed width but they haven't seemed to help.

Since iOS 9.0, the system font uses proportional digits. If you want monospaced digits, there's a variant font which you can obtain using +[UIFont monospacedDigitSystemFontOfSize:weight:]. This only works for the system font.

If you want to work with another font, you try to ask for a monospaced variant, but there may not be one. Given a UIFont, you can request its fontDescriptor, then ask that for a similar font descriptor that's monospaced (not just for digits) using -[UIFontDescriptor fontDescriptorWithSymbolicTraits:] and UIFontDescriptorTraitMonoSpace. You can then create a new font by passing the new font descriptor to +[UIFont fontWithDescriptor:size:].

However, I doubt there's a monospace variant of Impact. It's not suitable for your purpose.

swift, Since iOS 9.0, the system font uses proportional digits. If you want monospaced digits, there's a variant font which you can obtain using +[UIFont  Specify the maximum number of lines for the label to use when laying out the text with the number Of Lines property. Setting a value of 0 allows the label to use as many lines as necessary to lay out the text within the label’s width.

I had the same problem. @KenThomases answer works. Here's the Swift version:

// replace whatever font your using with this font instead to stop the shaking
UIFont.monospacedDigitSystemFont(ofSize: 19, weight: UIFont.Weight.regular)

ie:

yourLabel.font = UIFont.monospacedDigitSystemFont(ofSize: 19, weight: UIFont.Weight.regular)

FYI there are other UIFont.Weight weights:

.black, .bold, .heavy, .light, .medium, .regular, .semibold, .thin, .ultraLight

According to this other answer the fonts below are system generated fonts that are also monospaced so they won't shake either:

Courier

Courier-Bold

Courier-BoldOblique

Courier-Oblique

CourierNewPS-BoldItalicMT

CourierNewPS-BoldMT

CourierNewPS-ItalicMT

CourierNewPSMT

Menlo-Bold

Menlo-BoldItalic

Menlo-Italic

Menlo-Regular

ie:

// no shaking
yourLabel.font = UIFont(name: "Menlo-Regular", size: 19)

If your using just numeric digits then HelveticaNeue is also monospaced and it doesn't shake but it's questionable. Read the comments below this answer before using this font.

ie:

// no shaking but apparently you can only use numbers not letters
yourLabel.font = UIFont(name: "HelveticaNeue", size: 19)

Build a count down timer with Swift 3.0, Here you will see how to build a simple timer for iOS using Swift 3.0. I will assume (This label will show the starting time and the counting down in seconds, minutes and hours.) It will stop the timer, but not reset the current value of seconds. invalidate() stops the Timer from firing again and removes the Timer from the running loop. Instead of adding a selector function which gets triggered, we can use a closure to run the desired action whenever the Timer is fired. In the next section, we’ll be creating a Countdown Timer iOS Application with play/pause/reset buttons.

Use A monospaced font, also called a fixed-pitch, fixed-width, or non-proportional font, is a font whose letters and characters each occupy the same amount of horizontal space. Examples of monospaced fonts include Courier, Courier New, Lucida Console, Monaco, and Consolas

iOS Timer Tutorial, In this iOS Timer tutorial, you'll learn how timers work, affect UI You'll note that the seconds label does not increment at the moment. To avoid any side effects of decreased app responsiveness and increased power usage  Today, I will show how to build a timer that countdowns until an event (e.g. new year’s day or release date of a video game) using the scheduledTimer of the Timer class and refresh it every second. First, we set the current date using the calendar that user use on their device.

How to make a Countdown Timer in iOS using Swift, If you want to change the label after the event has passed, you can do this by checking if the current date is after the event. If yes, then will stop the  MZTimerLabel is a UILabel subclass, which is a handy way to use UILabel as a countdown timer or stopwatch just like that in Apple Clock App with just 2 lines of code. MZTimerLabel also provides delegate method for you to define the action when the timer finished.

WKInterfaceTimer, WKInterfaceTimer. A label that displays a countdown or count-up timer. SDK. I'd like to override UILabel's setText method but I'm not sure that:. A) it's possible, and B) if maybe there's a better way to achieve what I'm trying to accomplish. I have a subclass of UIView that has a UILabel property as one of its sub-views.

‎Timer+ on the App Store, Download Timer+ and enjoy it on your iPhone, iPad, and iPod touch. Create, label and reuse timers for anything – yoga stretches, timed quizzes, cooking rice,​  You can also use a timer to schedule a one time event for some time in the future. The main difference from the above example is that you use repeats: false instead of true. timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false)

Comments
  • give a fixed width constraint to a label with highest wide possibility . For ex 33:33:33 should completely be contained in it. Give label some background color. It will solve your problem to some extent
  • Using textkit you can specify that all numbers should be equal width.
  • And does this work for custom fonts too? In this case I'm using Impact
  • That was exactly what I meant with my comment, just couldn't find it fast enough ;)
  • @ChrisByatt this will not work for fonts which do not have a mono spaced digit set.
  • only Courier, Courier New are included in Xcode project. But yeah those fonts have equal width per any character. nice.