0

How should I handle text formatting in iOS? I'm not talking about Currency or Decimal styles. Since iOS doesn't have setFormat(), it's kind of complicated.

Two examples of the format I want: 123.456.789-10 and 123.456.789/1000-00.

My aim is to format a text field as the user types. Like any normal site does when, for example, you type a phone number. It doesn't comes from a model or anything. The user just needs to type the numbers and when necessary I'll insert the ".","/" or "-".

NSPunk
  • 502
  • 4
  • 14
  • 1
    Could you be a little more specific about what you are trying to do? How does the data in your model look, that ends up looking like the sample strings that you provided? – Sergey Kalinichenko Jul 27 '12 at 18:49
  • @dasblinkenlight You're right! Edited the question. – NSPunk Jul 27 '12 at 18:59
  • 1
    Have you looked at the [Data Formatting Guide](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html)? Have you [looked around on SO](http://stackoverflow.com/search?q=%5Bios%5D+formatting+text+field)? The top hit from that search: [Phone number formatting](http://stackoverflow.com/questions/6052966/phone-number-formatting). – jscs Jul 27 '12 at 19:10
  • @JoshCaswell Yes Josh! But as mentioned they were too extensive for just a simple text format plus, they all had some overhead! – NSPunk Jul 27 '12 at 19:16

2 Answers2

5

You can use regular expressions to do formatting, like this:

NSRegularExpression *fmt = [NSRegularExpression
    regularExpressionWithPattern:@"(.{1,3})(.{1,3})(.{1,3})(.{1,2})"
                         options:0
                           error:NULL];
NSMutableString *str = [NSMutableString stringWithString:@"12345678910"];
[fmt replaceMatchesInString:str
                    options:0
                      range:NSMakeRange(0, str.length)
               withTemplate:@"$1.$2.$3-$4"];

This transforms the string into 123.456.789-10

In order to apply this formatting dynamically as users type, create NSRegularExpression *fmt upfront, store it in your UITextFieldDelegate, and use it in your textField:shouldChangeCharactersInRange:replacementString: method.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • WOW!! That's what i'm talking about!! GREAT Answer! – NSPunk Jul 27 '12 at 19:15
  • @NSPunk Well, I have not tried using this for field formatting, so you may very well discover that it is not as performant as some simpler alternatives that do not rely on regular expressions. Anyway, give it a try, and let me know if it worked. If you are no longer looking for an improved answer, consider accepting one by clicking the outline of the check mark; this will earn you a brand-new badge on stack overflow, too. – Sergey Kalinichenko Jul 27 '12 at 19:21
1

I created my own method a couple days ago for checking for valid temperature input. It was a bit of a pain as well because I had to check for: -only numbers -only one decimal point -if there was a negative sign it had to be at the front

I ended up using [my string characterAtIndex:index]; and checking each character, since i would have no more than 4 characters.

EDIT:

Here is some pseudo code

for (every char in your string)
    if(the index of the char % 3 == 2) //every third char, 0,2,5...
        the char must be a . or / or -

    else
        the char must be a number
BloonsTowerDefence
  • 1,184
  • 2
  • 18
  • 43
  • Yeah.. that is my initial thinking too, but it's really painful just to think about it! – NSPunk Jul 27 '12 at 18:43
  • yes you would need to make sure that every 3rd character is a decimal, dash, or slash, which would take a lot of if statements. You may also be able to use `NSCharacterSets`, or if you know regular expressions (regex) you could use those. – BloonsTowerDefence Jul 27 '12 at 18:49
  • Come to think of it, it wouldnt be too many if/else statements... I've edited some psuedo code into my answer – BloonsTowerDefence Jul 27 '12 at 18:51
  • That's a good point, but I'm doing something a little different. The user just need to type the numbers and i'll insert the special characters as needed by the format. So i believe it's more easier to do things! – NSPunk Jul 27 '12 at 19:03