SpaceDog SpaceDog - 1 year ago 99
iOS Question

Aligning floats with two decimals by the point

I have a bunch of

inside a vertical
. The stack is making all labels with the same width and height.

Each label has a float in it, like this:

enter image description here

But this is ugly aesthetically. I want to align the numbers by the point. Like this:

enter image description here

One of my problem is that these floats are in different labels.

These numbers use this to format:

[NSString stringWithFormat:@"%.2f", value];

I don't have a clue if even it is possible. So, I have no code to show. Is it possible to do that? How?

EDIT: The floats are left aligned on the labels. Ok, I can reduce the labels width and align them to the right, but I am wondering if there is another solution in code because reducing the labels's width will cause other problems on the interface, crapping the whole thing.

Answer Source

If, as your edit says, you don't want to set your labels' textAlignment to right (which is certainly the easiest solution), here's another solution. You need to do two things:

  1. You need to use a font with monospaced digits. In iOS 9 and later, the system font does not use monospaced digits by default, but there are programmatic ways to get a variant of the system font that does use monospaced digits. You can use +[UIFont monospacedDigitSystemFontOfSize:weight:], or if you already have a UIFont object, you can get a monospaced-digits variant (if there is one) by adding an attribute to its font descriptor. See this answer for code to do this (in Swift). Unfortunately there is no way to do this in a storyboard or xib without code.

  2. You need to add enough U+2007 FIGURE SPACEs to the beginning of each label's text to make them all the same length. For example, if they should all be five characters long:

    NSString *text = [NSString stringWithFormat:"%.2f", value];
    while (text.length < 5) {
        text = [@"\u2007" stringByAppendingString:text];
    label.text = text;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download