I want to group the numbers entered into my amount field, using “.” as the grouping seperator, like 100.000
I implemented shouldChangeCharactersInRange method of my view controller so that only numbers and one comma is allowed for the decimal part, which can be only 2 digits.
Now I want to put the grouping seperator as the user enters the digits, in real time. When I think of the implementation, I am confused about things like the user deleting the digits or things like the user placing the cursor at the beginning and typing 0 in the front of the entered number, and so…
What would be the cleanest implementation?
OK, here is how:
First, you implement a
UITextFieldcategory.Then you say to yourself:
[self addTarget:self action:@selector(formatTextAsNumber) forControlEvents:UIControlEventEditingChanged];And here is the
formatTextAsNumbermethod that does the charm: