iphone - 为电话号码文本字段设置适当的键盘

标签 iphone ios keyboard uitextfield

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

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

最佳答案

你可以在你想添加的地方添加 - 你自己就像下面的代码将添加 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

关于iphone - 为电话号码文本字段设置适当的键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163795/

相关文章:

ios - 在键盘出现之前获取键盘高度?

iOS 7 m4a 到 mp3 编码 - 这可能吗?

ios - 将数组中的项目添加到特定索引处的不同数组

iphone - popToRootController 不工作

objective-c - Xcode建议将%C替换为%d

macos - 如何在 Mac OS X 的辅助功能 API 中获取当前键盘焦点坐标?

iphone - 神秘的UITableView卡住问题

ios - UISearchDisplayController 表格框架

iphone - 如何阻止 UIScrollView 水平弹跳?

javascript - 捕获 KeyCode 时出现键盘错误声音