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 "-".
You can use regular expressions to do formatting, like this:
This transforms the string into
123.456.789-10In order to apply this formatting dynamically as users type, create
NSRegularExpression *fmtupfront, store it in yourUITextFieldDelegate, and use it in yourtextField:shouldChangeCharactersInRange:replacementString:method.