Search code examples
objective-cnsstringuitextviewnsrangenscharacterset

uitextview shouldChangeTextInRange work in all method


I've input all the 3 methods in - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string

However, three of the following method can only work 1 of them, I wonder how should the code be written to make all 3 methods to work in this way?

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

//restrict user to type 70 characters
int limit = 69; 
return !([inputTextSection.text length]>limit && [string length] > range.length);

//restrict inputing uppercase to lowercase
NSRange uppercaseCharRange;
uppercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];

if (uppercaseCharRange.location != NSNotFound) {

    inputTextSection.text = [inputTextSection.text stringByReplacingCharactersInRange:range
                                                                           withString:[string lowercaseString]];

    return NO;
}

return YES;


// restrict user to input other characters
NSCharacterSet *unacceptedInput = nil;


if (textView == inputTextSection) {

    if ([[inputTextSection.text componentsSeparatedByString:@"@"] count] > 1) {
        unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet];

    } else {
        unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@"\n .,;:<>[]!$%&'*+-/=?^_{}()~@"]] invertedSet]; //if nextline needed, insert "\n"
    }
}
else {
    unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet];
}
// If there are any characters that I do not want in the text field, return NO.
return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);}

Solution

  • I've fixed it by the following method:

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string{
    
    if (range.location>=70){
        return  NO;
    }
    else
    {
        NSCharacterSet *unacceptedInput = nil;
    
        if (textView == inputTextSection || range.location>=70)  {
    
            NSRange uppercaseCharRange;
            uppercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
    
            if (uppercaseCharRange.location != NSNotFound) {
    
                inputTextSection.text = [inputTextSection.text stringByReplacingCharactersInRange:range
                                                                                       withString:[string lowercaseString]];
    
                return NO;
            }
    
    
            if ([[inputTextSection.text componentsSeparatedByString:@"@"] count] > 1) {
                unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@".-"]] invertedSet];
    
            } else {
                unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:[ALPHA_NUMERIC stringByAppendingString:@"\n .,;:<>[]!$%&'*+-/=?^_{}()~@"]] invertedSet]; 
            }
        }
        else {
            unacceptedInput = [[NSCharacterSet illegalCharacterSet] invertedSet];
        }
    
        return ([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] <= 1);
    
        return YES; 
        } 
    }