检测TextView + iphone sdk中“@”后写的字符

时间:2013-01-03 04:29:10

标签: iphone uitextview

在Text-view中我想检测“@”charachter之后写的字符

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

     }      
    return YES;

}

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

3 个答案:

答案 0 :(得分: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;

}

此处isCharacterBefore是在.h文件中声明的BOOL变量。 希望它对你有帮助。

答案 1 :(得分: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]);
   }
}
}

试试这个:)

答案 2 :(得分: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;
 }