目前大多数用来实现定时器显示的控件都是UILabel,显示验证码计数时出现跳动和闪烁的问题。
在iOS9以前系统默认的英文字体是Helvetica, 这种字体每个数字的宽度都是相等的。而在iOS9以后默认的英文字体变为San Fransico字体,这种字体每个数字的宽度是不相等的。
正是因为数字宽度的不相等就导致了用UILabel来显示定时器文本时出现文字跳动闪烁的问题。 因此解决的方案就是选用一种等宽数字字体显示即可。为此有两个解决方案:
1、使用Helvetica字体代码默认字体。
textLB = UILabel()
textLB.frame = CGRect(x: 0, y: 100, width: view.bounds.size.width, height: 100)
//设置字体
textLB.font = UIFont(name: "Helvetica", size: 30)
textLB.textAlignment = .center
textLB.text = "倒计时"
view.addSubview(textLB)
2、用UIFont的API:+ (UIFont *)monospacedDigitSystemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight;
textLB = UILabel()
textLB.frame = CGRect(x: 0, y: 100, width: view.bounds.size.width, height: 100)
//设置字体
//open class func monospacedDigitSystemFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont
textLB.font = UIFont.monospacedDigitSystemFont(ofSize: 30, weight: .regular)
textLB.textAlignment = .center
textLB.text = "倒计时"
view.addSubview(textLB)