One option is to overlay a UILabel
with a transparent background on top of your UITextField
. Register as the delegate
of the UITextField
and implement:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Then every time the user types, this method will be called allowing you to take the new value, convert it to an integer and pass it through an NSNumberFormatter, then update the UILabel to show this formatted value.
static NSNumberFormatter *formatter;
- (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *textFieldValue = [aTextField.text stringByAppendingString:string];
if (range.length > 0)
textFieldValue = [aTextField.text substringToIndex:range.location];
if ([textFieldValue isEqualToString:@""])
self.formattedLabel.text = textFieldValue;
else
{
NSDecimalNumber *newValue = [[NSDecimalNumber alloc] initWithString:textFieldValue];
if (!formatter) formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
self.formattedLabel.text = [formatter stringFromNumber:newValue];
}
return YES;
}