【问题标题】:set appropriate key pad for phone number text field为电话号码文本字段设置适当的键盘
【发布时间】:2013-04-16 08:09:22
【问题描述】:

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

如何设置包含'-'键的数字小键盘?

【问题讨论】:

  • 请尝试使用这个。我想你想要它。您可以使用Numbers and Punctuation 键盘

标签: iphone ios keyboard uitextfield


【解决方案1】:

您可以添加 - 您自己在要添加的位置,如以下代码将在文本字段中输入数字时添加 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;
}

Taken from here

【讨论】:

    【解决方案2】:

    我认为您可以使用UIKeyboardTypePhonePad 并将UITextFieldinputAccessoryView 属性用于- 按钮。

    代码:

    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:@"-"];
    }
    

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


    参考:

    inputAccessoryView

    当文本字段变为 第一响应者@property (readwrite, retain) UIView *inputAccessoryView;

    讨论

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

    Available in iOS 3.2 and later.
    

    在 UITextField.h 中声明

    参考UITextField Class

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多