0

I am having a slight issue at the moment regarding finding specific characters in a UILabel. I tried the simple

  if variable == "the string you want it to equal" {

     //Do the thing you want

  }

The issue is that my UIlabel updates based on the time. This normally would be no issue but I also have a smaller Label next to it as seconds.

This works fine until the main time label hits the double digits of 10, 11, 12. This causes the Label to expand and cuts off the smaller seconds label. I know I could place the smaller label somewhere where the main label doesn't cut it but the design wouldn't be as great.

I tried also to make variables as BOOLEANS

let time10 = "10"

let time11 = "11"

let time12 = "12"

Now this would require me to make many other variables like "10:01, 10:02" which would be highly inefficient.

Is it possible for a feature in swift to look up a specific amount of characters in a string (the first 2 in this case" and let me write the thing I want to do if the condition is met?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
RandomDude
  • 37
  • 1
  • 9

3 Answers3

0

Try using label.sizeToFit every time you update the label's value. Hopefully it should fix the issue.

kabiroberai
  • 2,930
  • 19
  • 34
  • Thanks for the suggestion. Unfortunately, this doesn't fix the issue. The main Label is still cutting the seconds off when in double digits. I have tried auto layout as well, but that doesn't seem to do anything. – RandomDude May 28 '15 at 04:51
0

To answer your original question, you can use the count function, for example you can do count(string), as explained in this answer.

Community
  • 1
  • 1
kabiroberai
  • 2,930
  • 19
  • 34
0

Try playing with .minimumScaleFactor and auto layout priorities. The first one will let one label scales down font size to avoid ....

With auto layout priorities you can make a label shrinks in spite of another.

It should be easier if you are using Interface Builder.

Hope this helps

Matteo Piombo
  • 6,688
  • 2
  • 25
  • 25