2

How do i stop text in a label from moving/changing size (being dynamic) when it has numbers that are changing every second.

var duration = //user input
func counting() {
    label.text = "\(duration)"
    if duration > 0 {
        duration -= 1
    } else if duration == 0 {
        //stop
}

The functions updates itself every second.

I have an label that have numbers counting down every second, but when it changes numbers the width changes to depending on how big the number is. How do i stop this and give every letter/number the same width. So i want an "1" to take as much space as an "5". I am not talking about the space between (margin) but rather the space inside (item/letter/number size).

Label text taking more space then if it had been 4:11

Label text taking less space then if it had been 3:56

Niklas
  • 357
  • 6
  • 20
  • Have you tried monospace font and whitespaces? – Yurii Kolesnykov Oct 23 '15 at 18:48
  • go to your storyboard and click this label.Then go to autoshrink on your attribute inspector and choose minimum font size. Tell me if that helped.By choosing the minimum size overtime you put any element it will just get smaller and the width of your label will always remain the same – Korpel Oct 23 '15 at 18:50
  • I have an width for the label, but the text inside the label is changing width. I want to stop that. – Niklas Oct 23 '15 at 18:53
  • you can programmatically use `var adjustsFontSizeToFitWidth:Bool` where it is inside your `UILabel` instance and if it's true every time the text gets bigger than you label size it will shrink automatically – Korpel Oct 23 '15 at 18:53
  • @Nik Does it change the label width or what? – Korpel Oct 23 '15 at 18:54
  • @Korpel the text width inside the label is still changing every time an new number is displayed. – Niklas Oct 23 '15 at 18:56
  • @Nik you want to remain the same width for 0 and the same for 10?Am i correct? – Korpel Oct 23 '15 at 18:57
  • @Korpel almost, the code always shows to numbers even if it is 01. I want 01 and 59 to take the same amount of space inside the label, so the width of every number is the same even if it really needs less space. (The reason the image does not show 01 is because i just added it to the code.) – Niklas Oct 23 '15 at 18:59

1 Answers1

2

After checking out monospaced fonts after a tip from @Yuri i have found a solution.

Here is an another question that shows you have to add monospace to a label in IOS 9, that i successfully fixed my problem with.

Niklas
  • 357
  • 6
  • 20