我正在使用UIBarButtonItem
时钟的标题部分。每次秒变化时,时钟都会根据秒部分的字符大小稍微向左/向右移动。
是否可以将标题文本左对齐或以某种方式使标题保持固定?
最佳:
@IBOutlet weak var RefreshButton: UIBarButtonItem!
在 ViewDidLoad 下:
let font = UIFont.systemFont(ofSize: 14)
RefreshButton.setTitleTextAttributes([NSAttributedString.Key.font: font], for: UIControl.State.normal)
RefreshButton.tintColor = UIColor.black
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ClockUTC), userInfo: nil, repeats: true)
目的:
@objc func ClockUTC(timer: Timer)
{
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:mm:ss"
timeFormatter.timeZone = TimeZone(abbreviation: "UTC")
let stringDate = timeFormatter.string(from: time)
RefreshButton.title = "UTC \(stringDate)"
}
预习: