Search code examples
swiftfont-sizecountdowntimer

Swift. An oversized caption in timerLabel


I created a countdown timer by DataComponentsFormatter. It works fine in certain languages. However, when I use it in English, the caption that appears after I tap "start button", are oversized like the photo below. Although I applied "adjustsFontSizeToFitWidth" to the label, it fixes only the size of the label before the start button is tapped.

If there is any function that I should use to make it a right size, would you advise me?

enter image description here

This is the timer I use for the label by the way.

func displayUpdate() -> Double {

    let setting = 300
    let formatter = DateComponentsFormatter()
    formatter.unitsStyle = .brief
    formatter.includesTimeRemainingPhrase = true
    formatter.allowedUnits = [.minute, .second]

    let remainCount = settings - count
    let outputString = formatter.string(from: remainCount)
    countDownLabel.text = outputString

     return (remainCount)
}

Solution

    1. Make sure your label has width constraints set so the label does not expand beyond the edge of the screen, this answer has an example or you can use the interface builder.

    2. Make sure the label's minimumScaleFactor property is set.

    This question has more details on auto shrinking label text.