In my iPhone app,there is a text field for entering phone number of user.I have tried number key pad,phone pad, decimal pad etc. But none is appropriate.
How to set a number key pad which contains the '-' key ?
In my iPhone app,there is a text field for entering phone number of user.I have tried number key pad,phone pad, decimal pad etc. But none is appropriate.
How to set a number key pad which contains the '-' key ?
You can add - yourself where you want to add like following code will add brackets (
and dash -
while entering numbers in textfield.
#pragma mark - Phone Number Field Formatting
// Adopted from: http://stackoverflow.com/questions/6052966/phone-number-formatting
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.mobileNumberField) {
int length = [self getLength:textField.text];
if(length == 10) {
if(range.length == 0)
return NO;
}
if(length == 3) {
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:@"(%@) ",num];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
}
else if(length == 6) {
NSString *num = [self formatNumber:textField.text];
textField.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]];
if(range.length > 0)
textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
}
}
return YES;
}
-(NSString*)formatNumber:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
if(length > 10) {
mobileNumber = [mobileNumber substringFromIndex: length-10];
}
return mobileNumber;
}
-(int)getLength:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
int length = [mobileNumber length];
return length;
}
I think you can use the UIKeyboardTypePhonePad
and use the inputAccessoryView
property of UITextField
for the -
button.
UIToolbar *punctuationbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"-" style:UIBarButtonItemStyleBordered target:self action:@selector(addHyphen)],
nil];
[numberToolbar sizeToFit];
_yourTextField.inputAccessoryView = punctuationbar;
And add the addHyphen
method like:
- (void)addHyphen
{
_yourTextField.text = [_yourTextField.text stringByAppendingString:@"-"];
}
Note: This method will append -
at the end of the text. You need to modify the above method.
inputAccessoryView
The custom accessory view to display when the text field becomes the first responder
@property (readwrite, retain) UIView *inputAccessoryView;
Discussion
The default value of this property is nil. Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text field becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard. Availability
Available in iOS 3.2 and later.
Declared In UITextField.h
Referece UITextField Class