xcode 4.2.1 - 限制 TextField 中的字符长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9461279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:26:56  来源:igfitidea点击:

xcode 4.2.1 - limiting character length in TextField

iphonexcodexcode4textfield

提问by Jan

I have been trying to limit a textField by many codes available out there in the internet but with no luck.

我一直试图通过互联网上可用的许多代码来限制 textField ,但没有运气。

I have added UIViewController<UITextFieldDelegate>in my header file

我已经UIViewController<UITextFieldDelegate>在我的头文件中添加了

and textField.delegate = self;in my viewDidLoad

textField.delegate = self;在我的viewDidLoad

and implemented the following fundtion in my .m file:

并在我的 .m 文件中实施了以下基金:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    return !([newString length] > 5);
}

this stil does not limit my text field. any idea?

这仍然不限制我的文本字段。任何的想法?

回答by Ilanchezhian

Do it as following as it has been the exact duplicate of this

请按照以下方式进行,因为它与完全相同

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 5) ? NO : YES;
}

回答by Darren

The replacement string is just the 1 character that was pressed, not the whole string so you need to add It to the current textfield.text before counting. Your count is probably always 1 (or more if pasting a word)

替换字符串只是被按下的 1 个字符,而不是整个字符串,因此您需要在计数之前将它添加到当前的 textfield.text 中。您的计数可能始终为 1(如果粘贴一个字则更多)

回答by Eduardo Iglesias

I implement this in my code and it works:

我在我的代码中实现了它并且它有效:

This code eliminate all the letters only accept numbers, but like I delete the character, you could delete everything that its over length 5 and it keeps a nice effect that appears and disappears

此代码消除所有字母只接受数字,但就像我删除字符一样,您可以删除长度超过 5 的所有内容,并保持出现和消失的良好效果

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:textField];
}


- (void)textChanged:(NSNotification *)textField {
    NSString *text = [[textField object] text];
    NSString *last = [text substringFromIndex:[text length] -1];
    NSArray *accept = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7" , @"8", @"9", @".", nil];

    for (int i=0; i<[accept count]; i++) {
        NSLog(@"%@", [accept objectAtIndex:i]);
        if (![last isEqualToString:[accept objectAtIndex:i]]) {
            [[textField object] setText:[text substringToIndex:[text length]-1]];
        }
    }
}