我在- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string
但是,以下三种方法只能使用其中的一种,我想知道如何编写代码以使所有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);}
答案 0 :(得分:0)
我已通过以下方法修复它:
- (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;
}
}