类微信 直接删除 @“用户名”操作

if ([text isEqualToString:@""]) { //删除@整个人名操作
        //从字符串分割到数组- componentsSeparatedByString:
        NSString *string = textView.text;
        NSLog(@"string:%@",string);
        NSRange range=self.textInputbar.textView.selectedRange;//光标位置
        if (range.location==string.length) {
            if (string.length>0) {
                NSArray *array = [string componentsSeparatedByString:@"@"];
                NSLog(@"array:%@",array);
                NSString *string2 = [string substringWithRange:NSMakeRange(string.length-1, 1)];
                if ([string2 isEqualToString:@" "]) {//只有最后一个是空格时执行删除整个人名
                    if (array.count>1) {//证明有@
                        for (int i=0; i<_notifyNameArray.count; i++) {
                            NSString * name=_notifyNameArray[i];
                            if (string.length>=name.length+2) {
                                NSString *string3 = [string substringWithRange:NSMakeRange(string.length-name.length-2, name.length+2)];//截取最后的可能人名的str   然后对比
                                NSString * str4=[NSString stringWithFormat:@"@%@ ",name];
                                if ([str4 isEqualToString:string3]) {//证明最后几个字是一个完整的@人名
                                    NSString *string5 = [string substringToIndex:string.length-name.length-2];
                                    textView.text=string5;
                                    return NO;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

posted @ 2016-12-02 17:43  frounk  阅读(370)  评论(0编辑  收藏  举报