试图限制iOS中UITextField的字符数

时间:2013-03-29 09:53:49

标签: ios uitextfield uitextfielddelegate

我的iOS应用程序中有一个UITextField,我已经在修改用户的输入。我现在意识到我还需要确保UITextField可以容纳一定数量的字符。我正在实现委托方法如下:

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

    if([[string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]]
        isEqualToString:@""])
        return YES;

    NSString *previousValue = [[[textField.text stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] stringByReplacingOccurrencesOfString:@"." withString:@""] stringByReplacingOccurrencesOfString:@"," withString:@""];
    string = [string stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
    NSString *modifiedValue = [NSString stringWithFormat:@"%@%@", previousValue, string];

    if ([modifiedValue length] == 1) {

        modifiedValue = [NSString stringWithFormat:@"0.0%@", string];

    }

    else if ([modifiedValue length] == 2) {

        modifiedValue = [NSString stringWithFormat:@"0.%@%@", previousValue, string];

    }

    else if ([modifiedValue length] > 2) {

        modifiedValue = [NSString stringWithFormat:@"%@.%@",[modifiedValue substringToIndex: modifiedValue.length-2],[modifiedValue substringFromIndex:modifiedValue.length-2]];

    }


    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithString:modifiedValue];
    modifiedValue = [formatter stringFromNumber:decimal];
    [textField setText:modifiedValue];

    return NO;

}

我不确定上面的代码如何考虑22个字符的限制。谁能看到我需要做什么?

4 个答案:

答案 0 :(得分:4)

尝试这样,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]<=22)
   return YES;
else
   return NO;
}

OR

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

return (textField.text.length <= 22);
}

答案 1 :(得分:2)

这是最适合我的解决方案,因为上述答案不允许您在达到最大字符后删除。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // allow adding of chars
    if (textField.text.length < MAX_CHARS) {
        return YES;
    }
    // allow deleting
    if (textField.text.length == MAX_CHARS && string.length == 0) {
        return YES;
    }

    return NO;
}

这不是最漂亮的解决方案,可能会被改进以处理其他情况,但这对我的需求很有效,并认为它可能对其他人有帮助。

答案 2 :(得分:0)

    if ((string.length == 0) || textField.text.length <= MAX_LIMIT)
        return YES;
    else
        return NO;

这是我发现完美的解决方案。

答案 3 :(得分:0)

试试这个: -

 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {


        print("chars \(textField.text!.characters.count) \( string)")

        if(textField.text!.characters.count > 20 && range.length == 0) {
            print("Please summarize in 20 characters or less")
            return false;
        }

        return true;
}