3つのメソッドすべてを入力しました- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
ただし、次の 3 つのメソッドはそのうちの 1 つしか機能しません。このように 3 つのメソッドすべてを機能させるには、どのようにコードを記述すればよいのでしょうか。
- (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);}