Solution to the problem of text jumping of timer displayed in uilabel

Time:2019-11-29

Let’s first look at a problematic display effect:

The GIF graph above will find that there is a problem of jitter and flicker when the verification code count is displayed. At present, most of the controls used to implement timer display are uilabel.

Before IOS 9, the default English font was Helvetica, in which the width of each number was equal. After IOS 9, the default English font changed to San Francisco font. The width of each number in this font is not equal.

It is precisely because of the unequal width of the numbers that the text of the timer is flickering when using the uilabel to display the text.Therefore, the solution is to select an equal width digital font to display. There are two solutions to this:

Default font with Helvetica font code.


 UILabel *label = [UILabel new];
 label.font = [UIFont fontWithName:@"Helvetica" size:16];

New API with UIFont:+ (UIFont *)monospacedSystemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight;

UILabel *label = [UILabel new];
 //Remember that this API will be effective only after IOS 9!!!
 label.font = [UIFont monospacedSystemFontOfSize:16 weight:UIFontWeightRegular];

summary

The above is the whole content of this article. I hope that the content of this article has some reference learning value for your study or work. Thank you for your support for developepaer.