Re: UITextField formatting for IP Address
Re: UITextField formatting for IP Address
- Subject: Re: UITextField formatting for IP Address
- From: Alex Kac <email@hidden>
- Date: Fri, 16 Oct 2009 19:54:22 -0500
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 (email@hidden)
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:
This email sent to email@hidden