Limiting the number of characters a user can enter does not work

I need to limit the number of characters that a user could enter in a text view. Im limiting this to 500 characters. Below is my code,

- (void)textViewDidChange:(UITextView *)textView{

    NSInteger restrictedLength=500;

    NSString *temp=textView.text;
    NSLog(@"hekk %ld",[[textView text]length]);
    if([[textView text] length] > restrictedLength){
        textView.text=[temp substringToIndex:[temp length]-1];
    }
}

The problem is that if the user edits a text image using the keyboard, then the user may not type more than 500 characters. But if the user copies the texts, the user can enter more than 500 characters. Can i deal with this?

+4
source share
7 answers

I used this code in my application, its work is excellent

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if([text isEqualToString:@"\n"])
        [textView resignFirstResponder];
    else
    {
        if (textView.text.length + (text.length - range.length) == 400) {

            [appDel displayAlert:@"Message limit is 400 character."];
        }
        return textView.text.length + (text.length - range.length) <= 400;

    }
    return YES;
}
+2
source

Use delegate method:

shouldChangeCharactersInRange

:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSInteger restrictedLength=500;

        NSString *temp=textView.text;
        NSLog(@"hekk %ld",[[textView text]length]);
        if(([[textView text] length] > restrictedLength)&&(range.length == 0)){
            return NO;
        }
        return YES;
    }
+3

shouldChangeTextInRange .

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        let textLenghtLimit = 100 
        let isChareactersLimitReached = (textView.text.characters.count - range.length + text.characters.count > textLenghtLimit)

        return !isChareactersLimitReached
    }
+2

.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    int maxCharacters = 500;
    NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if(newString.length > maxCharacters){
        textView.text = [newString substringToIndex:maxCharacters];
        return NO;
    }
    return YES;
}
+2

shouldChangeCharactersInRange UITextView .

, UITextView shouldChangeCharactersInRange, .

, :

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    NSInteger restrictedLength=500;

    NSString *temp=textView.text;
    NSLog(@"hekk %ld",[[textView text]length]);
    if([[textView text] length] > restrictedLength){

        textView.text=[temp substringToIndex:restrictedLength];

        return NO;
    }

    return YES;
}
+1

100

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
    {
        if(textField.text.length < 100)
        {
              return true;
        }
        return false;

    }
+1

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if(range.length + range.location > textView.text.length)
    {
      return NO;
    }

    NSUInteger newLength = [textView.text length] + [text length] - range.length;
    return newLength <= 500;
 }
+1

All Articles