In my latest update to Pedometer++ I had to remove the feature where you could badge your app’s icon with your current step count. In doing so I also removed one of the all time favorite hacks I’ve ever written. I felt that this deserved a little send off before it departs off into my Git history.
First, the back story. During the introduction of iOS 7 one of the random side effects of the wide reaching changes to the iOS user interface was that app icon badges started to get truncated whenever they got above 4 digits long.
This usually isn’t a problem since if you have more than (say) 10,000 unread emails you are likely long past caring about the precise number anymore. But for my little hack of using the badge to show specific counts this caused a problem.
At first, I thought I’d have to remove the feature in iOS 7, until one fateful afternoon (while taking a shower, of course) I had the insight that I might be able to work around this. My realization was that the numbers were getting truncated based on their displayed width and not their digit count. Thus a number with a lot of
1s in it would not get truncated because the
1s are so thin, whereas a number with a lot of
4s in it would be truncated because they render much wider.
So I then set out to alter the numbers that were displayed so that if I thought it would get truncated I would replace the least significant digits with
1s until it would no longer be truncated. After a bit of experimentation I found that I needed for a 5 digit number to contain at least two
1s in it in order to be assured that the number wouldn’t truncate. Usually, but not always, the first of these would be the leading digit since getting above 20,000 steps is quite a full day of walking.
Kind of awkwardly though, I realized that in order to achieve this I’d have to calculate how many
1s there were in the number. I poked a round a bit on the mathematical side of this but couldn’t work out a way to count how many
1s there were in a given number via mathematical means. There might be a way to do this, but I couldn’t find it.
So instead I figured that I could just write a really crude method using strings. I just cast the number to a string, then compared its length to a string where I replace all the
1 characters with the empty string. From here I could work out if I should change the last digit to a
1 (in the case of it already having another
1), or if I should replace the last two digits (if no other
1s are found).
Here is the final result:
I’ll be sad to see this little hack go. While it might sound a bit funny to have a favorite line of code, I think this was mine. I just loved how silly of a hack it was, but yet how effective it proved to be.
Though one small consolation for it being gone now is that I can publish it here and the world can see it for all of its silly goodness.
P.S. And yes, if you are wondering if I really did just have that inscrutable block of code entirely undocumented in the app…I did. I suppose that’s the blessing and curse of being an independent developer. 😇