[英]Swift 3 - DateComponents to Label.Text sometimes displays “…”
這讓我發瘋。
我試圖找到當前時間和設定時間的兩倍,最終將兩者從服務器中獲取。
這是現在沒有參數的時間getTimeDifference()
函數。
let formatter = DateFormatter()
let userCalender = Calendar.current;
let requestedComponent : Set<Calendar.Component> = [.month,.day,.hour,.minute,.second]
public func getTimeDifference() -> DateComponents
{
formatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"
let _startTime = Date()
let _endTime = formatter.date(from: "12/05/2016 14:01:00 a")
let timeDifference = userCalender.dateComponents(requestedComponent, from: _startTime, to: _endTime!)
return timeDifference
}
這就是我的HomepageViewController中的內容
@IBOutlet weak var labelHours: UILabel!
@IBOutlet weak var labelMinutes: UILabel!
@IBOutlet weak var labelSeconds: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCountdown), userInfo: nil, repeats: true).fire()
// Do any additional setup after loading the view.
}
func updateCountdown() -> Void {
let time = Countdown().getTimeDifference()
labelHours.text = time.hour?.description
labelMinutes.text = time.minute?.description
labelSeconds.text = time.second?.description
}
每當我運行此代碼時,它可能會在幾秒鍾內完全正常運行,但是它會顯示一些間隔...
問號(?)產生上面的圖像,而刪除(?)會將所有標簽設置為...
labelHours.text = time.hour?.description
labelMinutes.text = time.minute?.description
labelSeconds.text = time.second?.description
labelHours.text = time.hour.description
labelMinutes.text = time.minute.description
labelSeconds.text = time.second.description
使框架適合文本的大小。 您正在尋找的是:
questionsLabel.adjustsFontSizeToFitWidth = true
上面的代碼將更改字體大小以適合標簽的寬度。 並添加
questionsLabel.textAlignment = .center
它將在標簽中正確顯示文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.