-1

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);}
4

1 に答える 1

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; 
    } 
}
于 2012-10-08T13:09:25.667 に答える