Here is my code, btw. It works OK, but it still has the issue of moving the cursor to the end. Perhaps there is no way to do this on the iPhone - I just don't want to bang my head for hours.

- (void)formatForIP:(UITextField*)textField string:(NSString*)string
{
long long ip = [[string stringByReplacingOccurrencesOfString:@"." withString:@""] longLongValue];
        
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
        [numberFormatter setPositiveFormat:@"###,###,###,###"];
        [numberFormatter setGroupingSize:3];
        [numberFormatter setSecondaryGroupingSize:3];
        [numberFormatter setGroupingSeparator:@"."];
        [numberFormatter setUsesGroupingSeparator:YES];

textField.text = [numberFormatter stringFromNumber:[NSNumber numberWithLongLong:ip]];
        [numberFormatter release];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string
{
        if (textField.tag == 1)
        {
                if ([textField.text length] == 15)
                        return NO;
                else if ([textField.text length])
                {
NSString* newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

                        [self formatForIP:textField string:newString];
                        return NO;
                }
        }
        return YES;
}


On Oct 16, 2009, at 7:26 PM, Alex Kac wrote:

On desktop Cocoa I can put in a custom formatter on a textfield and get what I'm looking for. On iPhone, I can say I'm a bit confused as to the best way to handle this.

Given a UITextField and my desire to have the user type in an IP address, it sounds like it could be simple, but I can't figure it out.

Using

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string

works fine if I'm assuming the user is simply typing in the text going forward. The problem is if a user edits the text field then they lose their place as I set the text.

So my guess to this is that no I cannot do this (though I'm sure I've seen it on the iPhone), but would there be a good way to handle this properly?

"There will always be death and taxes; however, death doesn't get worse every year."
-- Anonymous





Alex Kac - President and Founder
Web Information Solutions, Inc.

"I am not young enough to know everything."
--Oscar Wilde




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to