1

哪个计数最大字符在UITextFieldUITextView?这对我来说真的很有趣!我有一些textfieldstextviews。如果我没有为其设置最大值,那么其中可以包含哪些计数字符?

4

1 回答 1

2

您需要实现UITextFieldDelegateUITextViewDelegate 方法来获取字符数

试试这个代码

对于 UITextView 的

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSUInteger enteredTextLength; // Variable to store the length on entered string.

    if(textView == txtViewName) {
        enteredTextLength = [textView.text length] + [text length] - range.length;

        NSLog(@"Text Length = %d", enteredTextLength);

        // To check number of characters entered.
        if (enteredTextLength < 10) {
            return YES;
        }
        else {
            return NO;
        }
    }
    else {
        return YES;
    }
}

对于 UITextField 的

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

    NSUInteger enteredTextLength; // Variable to store the length on entered string.

    if(textField == txtAlpha) {
        enteredTextLength = [textField.text length] + [string length] - range.length;

        NSLog(@"Text Length = %d", enteredTextLength);

        // To check number of characters entered.
        if (enteredTextLength < 10) {
            return YES;
        }
        else {
            return NO;
        }

    }
    else {
        return YES;
    }
}
于 2013-07-01T10:47:21.813 回答