Let’s start with a questionable demo:

The GIF above shows the problem of jumping and flickering when displaying the captcha count. At present, most of the controls used to realize the display of timers are UILabel.

Before iOS9, the default font was Helvetica, where each digit was the same width. After iOS9, the default font will be San Fransico, where the width of each digit is not equal.

It is the unequal number widths that cause the flickering of text when displaying timer text with UILabel. Therefore, the solution is to choose a uniform width digital font display. There are two solutions to this:

  1. Code the default font with Helvetica.
   UILabel *label = [UILabel new];
   label.font = [UIFont fontWithName:@"Helvetica" size:16];
Copy the code
  1. With the new API: UIFont + (UIFont *) monospacedDigitSystemFontOfSize: (CGFloat) fontSize weight (UIFontWeight) weight;
UILabel *label = [UILabel new]; // Remember that this API is available after iOS9!! label.font = [UIFont monospacedDigitSystemFontOfSize:16 weight:UIFontWeightRegular];Copy the code