2016-12-04 5 views
1

이것은 제 정신을 차게합니다.스위프트 3 - Label.Text의 DateComponents가 때때로 "..."을 표시합니다.

저는 현재 시간의 두 배와 결국 서버에서 얻을 수있는 설정 시간의 차이를 찾으려고합니다.

이것은 매개 변수가없는 순간에 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 
} 

을에있는 것입니다 ...

Working Not working

물음표 (?)는 이미지 ab 비켜 및 제거 (?)가 ...

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 
+5

각 UILabel은 두 자릿수의 너비가 충분한가요? 예를 들어 "24"는 "11"보다 너비가 더 필요합니다. – OOPer

답변

2

는 프레임이 텍스트의 크기에 맞게 만들면서 모든 레이블을 설정합니다. 당신이 찾고있는 것은 :

questionsLabel.adjustsFontSizeToFitWidth = true 

위의 코드는 레이블 너비에 맞게 글꼴 크기를 변경합니다. 또한 추가하십시오

questionsLabel.textAlignment = .center 

레이블에 텍스트를 올바르게 렌더링합니다.