如何在用户编辑UITEXTFIELD时自动插入字符串

时间:2014-01-06 17:46:01

标签: ios objective-c uitextfield uitextfielddelegate

我希望我的uitextfield在输入结束时就像XXX.XXX.XXX/XX。

为了限制长度,我使用它:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
    if (textField == _cpfField) {

        NSUInteger newLength = [textField.text length] + [string length] - range.length;
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT));
    } else{
        return YES;
    }

}

问题是如何插入“。”用户仍然在编辑它时使用“/”。

3 个答案:

答案 0 :(得分:12)

以下代码应执行以下操作:

  1. 限制可以键入/粘贴到文本字段中的字符数
  2. 在适当的位置自动添加句点和斜杠
  3. 防止用户复制/粘贴已经具有必要句点/斜杠的字符串的问题
  4. 尽管如此,几乎可以肯定有更有效的方法来做到这一点;但是,如果你不关心代码长度,那就可以了。但/ / p>

    - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        NSString *text = textField.text;
    
        // If we're trying to add more than the max amount of characters, don't allow it
        if ([text length] == 14 && range.location > 13) {
              return NO; 
        }
    
        // First lets add the whole string we're going for
        text = [text stringByReplacingCharactersInRange:range withString:string];
    
        // Now remove spaces, periods, and slashes (since we'll add these automatically in a minute)
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];
        text = [text stringByReplacingOccurrencesOfString:@"/" withString:@""];
    
        // We need to use an NSMutableString to do insertString calls in a moment
        NSMutableString *mutableText = [text mutableCopy];
    
        // Every 4th char will be a '.', but we don't want to check more than the first 8 characters
        for (NSUInteger i = 3; i < mutableText.length && i < 8; i += 4) {
            [mutableText insertString:@"." atIndex:i];
        }
        // If the text is more than 11 characters, we also want to insert a '/' at the 11th character index
        if (mutableText.length > 11) {
            [mutableText insertString:@"/" atIndex:11];
        }
    
        // lets set text to our new string
        text = mutableText;
    
        // Now, lets check if we need to cut off extra characters (like if the person pasted a too-long string)
        if (text.length > 14) {
            text = [text stringByReplacingCharactersInRange:NSMakeRange(14, mutableText.length-14) withString:@""];
        }
    
        // Finally, set the textfield to our newly modified string!
        textField.text = text;
    
        return NO;
    }
    

答案 1 :(得分:3)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = textField.text;
    text = [text stringByReplacingCharactersInRange:range withString:string];
    text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];

    // Do your length checking here

    NSMutableString *mutableText = [text mutableCopy];

    // Every 4th char will be a .
    for (NSUInteger i = 3; i < mutableText.length; i += 4) {
        [mutableText insertString:@"." atIndex:i];
    }

    textField.text = mutableText;

    return NO;
}

答案 2 :(得分:1)

  

英国国家保险号码文本字段

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == _txtNationalInsuranceNumber) {
        NSString *text = textField.text;

        // If we're trying to add more than the max amount of characters, don't allow it
        if ([text length] == 13 && range.location > 12) {
            return NO;
        }

        // First lets add the whole string we're going for
        text = [text stringByReplacingCharactersInRange:range withString:string];

        // Now remove spaces, periods, and slashes (since we'll add these automatically in a minute)
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];


        // We need to use an NSMutableString to do insertString calls in a moment
        NSMutableString *mutableText = [text mutableCopy];

        // Every 4th char will be a '.', but we don't want to check more than the first 8 characters
        for (NSUInteger i = 2; i < mutableText.length && i < 10; i += 3) {
            [mutableText insertString:@" " atIndex:i];
        }
        // If the text is more than 11 characters, we also want to insert a '/' at the 11th character index
        if (mutableText.length > 11) {
            [mutableText insertString:@" " atIndex:11];
        }

        // lets set text to our new string
        text = mutableText;

        // Now, lets check if we need to cut off extra characters (like if the person pasted a too-long string)
        if (text.length > 14) {
            text = [text stringByReplacingCharactersInRange:NSMakeRange(14, mutableText.length-14) withString:@""];
        }

        // Finally, set the textfield to our newly modified string!
        textField.text = text;

        return NO;
    }
    else
    {
        return  YES;
    }


}