如何使用Swift中的字符串类型删除时钟的运动时间数字?
问题描述:
我的代码显示时钟时间与timeLabel:UILabel
在我的应用惠特Swift3。如何使用Swift中的字符串类型删除时钟的运动时间数字?
那就是:
override func viewWillAppear(_ animated: Bool) {
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.currentTime), userInfo: nil, repeats: true)
}
func currentTime(){
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)
if second % 2 == 0{
timeLabel.text = "\(hour):\(minutes)"
}else {
timeLabel.text = "\(hour) \(minutes)"
}
但当两点小时和分钟数之间消失了,他们是一点点移动。如何解决这个问题?
答
用一招可以解决这个问题。
通过在else
条件添加具有相同的背景颜色另一个标签,烨
对于夫特3:
var attributes = [String: AnyObject]()
attributes[NSForegroundColorAttributeName] = self.view.backgroundColor
let attributedString = NSAttributedString(string: ":", attributes: attributes)
label.attributedText = attributedString