Batool Mustafa
Batool Mustafa

Reputation: 21

White spaces in Objective-c

I added a feedback box inside my IOS application, and I want it to take only text to submit the response from the user, but when I tried to enter a white spaces inside the box it took it as a text and accept the submitting! How can I prevent that?

Upvotes: 1

Views: 68

Answers (1)

Chirag Shah
Chirag Shah

Reputation: 3016

Specify the UIViewController as the delegate to your text view (you can do this either programmatically or specify the delegate in Interface Builder); and

Your UITextViewDelegate method shouldChangeTextInRange needs to check to see if the string to be inserted contains a space:

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

Upvotes: 2

Related Questions