1

我的 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

4 回答 4

4

试试这样

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

或者

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

return (textField.text.length <= 22);
}
于 2013-03-29T09:57:48.890 回答
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;
}

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

于 2014-04-20T14:41:09.447 回答
0
    if ((string.length == 0) || textField.text.length <= MAX_LIMIT)
        return YES;
    else
        return NO;

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

于 2016-03-23T05:35:41.813 回答
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;
}
于 2016-08-16T11:13:56.317 回答