0

在文本视图中,我想检测写在“@”字符之后的字符

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([txt_comment.text isEqualToString:@"@"]){

     }      
    return YES;

}

但是一旦我写了“@”字符,我就能捕捉到这个事件。如何检测事件和写在“@”后面的字符?

4

3 回答 3

2
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"%@",text);
    if (isCharacterBefore) {
        NSLog(@"the Character after @ is %@",text);
        isCharacterBefore = NO;
    }
    if([text isEqualToString:@"@"]){
        isCharacterBefore = YES;
    } 

    return YES;

}

isCharacterBeforeBOOL在 .h 文件中声明的变量。希望它对你有帮助。

于 2013-01-03T04:45:29.883 回答
1
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([txt_comment.text rangeOfString:@"@"].location == NSNotFound) 
{
   NSLog(@"string does not contain @");
}
else 
{
   NSLog(@"string contains @!");
   /* What ever you want to do Write here */
   NSArray *tempArray = [txt_comment.text componentsSeparatedByString:@"@"];
   if([tempArray count] >= 2 && [tempArray objectAtIndex:1]! = NULL)
   {
      NSLog(@"%@",[tempArray objectAtIndex:1]);
   }
}
}

尝试这个 :)

于 2013-01-03T04:35:51.900 回答
0

componentsSeparatedByString:在你的字符串中使用

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
   NSArray *arr = [text componentsSeparatedByString:@"@"];
   if([arr count] == 2)
      NSLog(@"Your String After @ %@",[arr objectAtIndex:1]);
   else
       NSLog(@"@ string not found");

   return YES;
 }
于 2013-01-03T04:37:10.060 回答