在Text-view中我想检测“@”charachter之后写的字符
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([txt_comment.text isEqualToString:@"@"]){
}
return YES;
}
但是一旦我写了“@”字符,我就能抓住这个事件。如何检测事件和“@”后写的字符?
答案 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;
}