How to limit characters in UITextView iOS

I have a UITextView and I would like to limit the number of characters the user can enter. I have also tried to display a warning if the textbox is empty, but no luck.

textview.h

@property (nonatomic,strong) IBOutlet UITextView *textField;
@property (nonatomic, retain) NSString *userText;

textview.m

- (IBAction)next:(id)sender {

    self.userText = self.textField.text;

    if ([self.textField.text length] == 0) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                                message:@"Enter some text"
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
}

      

here is the text limit that doesn't work,

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

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

    } else {

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return newLength <= 70;
    return (textField.text.length <= 70);
}
}

      

+3


source to share


3 answers


1) add UITextViewDelegate to your view manager like this

@interface ViewController : UIViewController <UITextViewDelegate>

      

Each time the user types a character on the keyboard, the following method is displayed before the character. This is a handy place to check the characters that the user enters and to disallow certain characters that you want to restrict.

TextView: shouldChangeCharactersInRange: replacementString

2) then write the following code. instead of 140 you can put any number you want.



- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return textView.text.length + (text.length - range.length) <= 140;
}

      

For more information see UItextView

Swift 2.0 solution

 func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    return textView.text.characters.count + (text.characters.count - range.length) <= textViewLimit
}

      

+9


source


try it



 - (BOOL)isAcceptableTextLength:(NSUInteger)length {
    return length <= 70;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string {
    return [self isAcceptableTextLength:textField.text.length + string.length - range.length];
}

-(IBAction)checkIfCorrectLength:(id)sender{
    if (![self isAcceptableTextLength:self.textField.text.length]) {
        // do something to make text shorter
    }
}

      

0


source


you can use below code to restrict text characters in first method, you can set limited length for textbox, or if you passed text that is larger than your restricted text, it will create a substring of that string to correct length for textview.

#pragma mark - Textview Delegate
 -(void)textViewDidChange:(UITextView *)textView
 {   
    NSString *temp = textView.text;
   titleLength = textView.text.length;
   if (titleLength > RESTRICTED_LENGHT ) 
    {
    textView.text = [temp substringToIndex:RESTRICTED_LENGHT];
    }
 }

      

or you can use this delegate method and return None if the text view text is longer than your limited length.

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
     if([text length] == 0)
      {
        if([textView.text length] != 0)
        {
          return YES;
        }
     }
   else if([[textView text] length] > 139)
    {
       return NO;
    }
   return YES;
}

      

0


source







All Articles