在UITextField的开头添加常量国家/地区代码

时间:2015-01-14 11:29:54

标签: ios objective-c iphone uitextfield

我有一个UITextField,用户需要在其中输入电话号码。

这就是现在的样子:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Auto-add hyphen before appending 4rd or 7th digit
    //
    //
    if (range.length == 0 && (range.location == 3 || range.location == 7))
    {
        textField.text = [NSString stringWithFormat:@"%@-%@", textField.text, string];
        return NO;
    }

    // Delete hyphen when deleting its trailing digit
    //
    //
    if (range.length == 1 && (range.location == 4 || range.location == 8))
    {
        range.location--;
        range.length = 2;
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@""];
        return NO;
    }

    //  Prevent crashing undo bug – see note below.
    //
    //
    if (range.length + range.location > textField.text.length)
    {
        return NO;
    }

    //  Limit text field characters
    //
    //
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 12) ? NO : YES;
}

在第3位之后,我正在添加连字符而不是。我在这里想要实现的是在UITextField的开头添加一个国家代码作为常量,并且用户将无法将其删除。让我们说美国国家代码,然后UITextField文本在开始+1-时看起来就像那样,然后在写完整数之后会看起来像这样:+1-600-242-252

我该怎么做?

提前致谢!

2 个答案:

答案 0 :(得分:0)

此答案假设一个起始国家/地区代码字符串,其中包含末尾的连字符,例如:self.countryCode = @"+1-";。文本字段最初应包含“+1 - ”。

我的回答方式比你原来的意图更全面,因为它处理了你忽略的许多用例,例如复制和粘贴操作有多个字符,不合适的连字符删除,不适当的连字符添加,中线插入,但它仍然不完美,因为你的原始答案在某些方面是非特定的...例如,如果你指定用户只能输入数字,那么代码可以更清晰。

以下实施在整个评论中逐行描述。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Combine the new text with the old
    NSMutableString *combinedText = [[textField.text stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%@", string]] mutableCopy];

    // If the user deletes part of the country code or tries
    // to edit it in any way, don't allow it
    if (combinedText.length < self.countryCode.length ||
        ![combinedText hasPrefix:self.countryCode]) {
        return NO;
    }

    //  Limit text field characters to 12
    if (combinedText.length > self.countryCode.length + 12) {
        return NO;
    }

    // If the user tries to add a hyphen where there's supposed
    // to be a hyphen, allow them to do so.
    if ([string isEqualToString:@"-"] &&
        (range.location == self.countryCode.length + 3 ||
        range.location == self.countryCode.length + 7)) {
        return  YES;
    }

    // Remove all the hyphens other than the one directly
    // following the country code        
    [combinedText replaceOccurrencesOfString:@"-" withString:@"" options:0 range:NSMakeRange(self.countryCode.length, [combinedText length] - self.countryCode.length)];

    // Auto-add the hyphens before the 4th and 7th digits
    if (combinedText.length > self.countryCode.length + 3)
        [combinedText insertString:@"-" atIndex:self.countryCode.length + 3];
    if (combinedText.length > self.countryCode.length + 7)
        [combinedText insertString:@"-" atIndex:self.countryCode.length + 7];

    // Store the original cursor position
    UITextPosition *pos = [textField selectedTextRange].start;

    // Count up the original number of hyphens
    NSUInteger originalNumberOfHyphens = [[textField.text componentsSeparatedByString:@"-"] count] - 1;
    // Count up the new number of hyphens
    NSUInteger newNumberOfHyphens = [[combinedText componentsSeparatedByString:@"-"] count] - 1;

    // Create a cursor offset to reflect the difference
    // in the number of hyphens
    float offset = newNumberOfHyphens - originalNumberOfHyphens;

    // Update the text field to contain the combined text
    textField.text = combinedText;

    // Update the cursor position appropriately
    if (string.length > 0) {
        UITextPosition* cursor = [textField positionFromPosition:[textField beginningOfDocument] offset:range.location + range.length + offset + string.length];
        textField.selectedTextRange = [textField textRangeFromPosition:cursor toPosition:cursor];
    } else {
        UITextPosition* cursor = [textField positionFromPosition:pos inDirection:UITextLayoutDirectionLeft offset:1-offset];
        textField.selectedTextRange = [textField textRangeFromPosition:cursor toPosition:cursor];
    }

    // No need to replace the string since it's already been done
    return NO;
}

答案 1 :(得分:-1)

要在开头保持常量,您基本上想要检查建议的文本中是否仍然存在常量。如果它不拒绝这样的编辑。

您不应尝试在特定编辑步骤中插入连字符。操纵整个字符串会更好。

E.g。

  1. 测试字符串是否有效。即starts with +1
  2. 删除您之前添加的所有连字符
  3. 重新插入所有连字符
  4. 在代码中,这将是这样的:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.textField.text = @"+1"; // start with a +1 in the textField otherwise we can't change the field at all
    }
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        NSString *proposedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
        if (![proposedText hasPrefix:@"+1"]) {
            // tried to remove the first +1
            return NO;
        }
        NSString *formattedPhoneNumber = [proposedText substringFromIndex:2]; // without +1 prefix
        NSString *unformattedPhoneNumber = [formattedPhoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""]; // without hypens
    
        // start with the prefix
        NSMutableString *newText = [NSMutableString stringWithString:@"+1"];
    
        for (NSInteger i = 0; i < [unformattedPhoneNumber length]; i++) {
            if (i % 3 == 0) {
                // add a - every 3 characters. add one at the beginning as well
                [newText appendString:@"-"];
            }
            // add each digit from the unformatted phonenumber
            [newText appendString:[unformattedPhoneNumber substringWithRange:NSMakeRange(i, 1)]];
        }
        textField.text = newText;
        return NO;
    }
    

    这仍然是一个非常天真的实施。它有几个问题,例如光标将始终在最后,因为我们手动设置textField的text。因此用户无法轻松删除字符串中间的数字。当然有办法解决这个问题。 selectedTextRange将是要使用的财产。而且你无法真正将电话号码粘贴到现场。当然,用户无法删除连字符。

    用户键入时格式化往往会很快变得复杂,因为边缘情况太多了。但这应该让你开始。