0

在我的 iPhone 应用程序中,有一个用于输入用户电话号码的文本字段。我尝试过数字键盘、电话键盘、小数键盘等。但没有一个是合适的。

如何设置包含“-”键的数字键盘?

4

2 回答 2

2

您可以添加 - 您自己想要添加的位置,如以下代码将添加brackets ( 以及dash -在文本字段中输入数字时。

#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;
}

取自这里

于 2013-04-23T08:00:17.697 回答
2

我认为您可以使用UIKeyboardTypePhonePad和 使用按钮的inputAccessoryView属性。UITextField-

代码:

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;

并添加如下addHyphen方法:

- (void)addHyphen
{
  _yourTextField.text = [_yourTextField.text stringByAppendingString:@"-"];
}

注意:此方法将附加-在文本的末尾。您需要修改上述方法。


参考:

输入附件视图

当文本字段成为第一响应者时显示的自定义附件视图@property (readwrite, retain) UIView *inputAccessoryView;

讨论

此属性的默认值为 nil。当文本字段成为第一响应者时,将视图分配给此属性会导致该视图显示在标准系统键盘上方(或自定义输入视图上方,如果提供的话)。例如,您可以使用此属性将自定义工具栏附加到键盘。可用性

Available in iOS 3.2 and later.

在 UITextField.h 中声明

参考UITextField 类

于 2013-04-23T08:17:32.117 回答