How can I not allow "space" to be entered in UITextView?

I have a UITextView and I don't want the user to have spaces in the typed text. What should I do to prevent him from using the space bar? Thank!

+3


source to share


3 answers


You need



  • Specify the view controller as delegate

    in the textual view (you can either do this programmatically or specify a delegate in the Interface Builder); and

  • In your method, UITextViewDelegate

    shouldChangeTextInRange

    you need to check if the line to be inserted contains:

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
        if ([text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].location != NSNotFound) {
            return NO;
        }
        return YES;
    }
    
          

    Note that this does not check if the value replacementText

    matches a space, because that is not enough validation. Instead, it checks to see if any space occurs anywhere in the replacement text. This is an important distinction because it is possible to insert text into a text representation that may not equal a space, but may contain a space somewhere in the inserted value.

+11


source


I think the correct way to do this is to prevent editing in the first place:

In your ViewController.h file, make it implement the UITextViewDelegate protocol:

@interface ViewController : UIViewController <UITextViewDelegate>

      

In the ViewController method viewDidLoad in ViewController.m, set the textField delegate to the view controller:



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.myTextView.delegate = self;
}

      

Finally, we need to commit the changes as they occur and remove the spaces. We can do this in the textViewDidChange: method. Returning NO in the shouldChangeTextInRange method: when there are spaces in the new line, the user will not insert text that has spaces in it (this may not be what you want). If we just remove the spaces, the user won't be able to enter the new space from the keyboard, but if they paste with something like "hello world" on the clipboard, they'll get "helloworld" in the TextView:

- (void)textViewDidChange:(UITextView *)textView
{
    // eliminates spaces, including those introduced by autocorrect
    if ([textView.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]].location != NSNotFound) {
        textView.text = [textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
}

      

+1


source


How do the "shouldChangeTextInRange" and "stringByReplacingCharactersInRange" methods work?

Just don't allow spaces aka @ "" with the method mentioned in the link (

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
 if( [string isEqualToString:@" "] )
    return NO;
 else 
    return YES;
}

      

Don't forget to set the text view delegate

-1


source







All Articles