等宽字体的小妙用

0 阅读1分钟

工作中有时会碰到如图所示的需求,timeLabel在不断更新时间过程中,导致左侧的button也在不断晃动,给用户的体验很差

ScreenRecording_06-06-2025 15-01-44_1.gif

怎么解决呢?

将右侧的timeLabel字体设置为等宽字体,即可解决

let baseFont = UIFont.systemFont(ofSize: 13)
let descriptor = baseFont.fontDescriptor.addingAttributes([
            .featureSettings: [[
                UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,
                UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector
            ]]
        ])
timeLabel.font = UIFont(descriptor: descriptor, size: 13)

ScreenRecording_06-06-2025 14-59-52_1.gif