模仿qq艾特功能
以下是使用方法
- (void)presentPersonListViewController {__weak typeof(self) weakSelf = self;//下面是写死的选中的人员,后续是人员列表回调for (int i = 0; i < 10; i++) {YYPersonItem *person = [[YYPersonItem alloc] init];person.name = [NSString stringWithFormat:@"张三+%d",i];NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:self.contentView.font.pointSize]};NSMutableArray *currentSelectedPersonItems = [[self.contentView.attributedText getCurrentAtPersonItems] mutableCopy];[currentSelectedPersonItems addObject:person];//这里可以判断去重...(根据自己需求情况而定)//将人的名字转化为NSTextAttachment[[YYTextAttachmentManager getInstance] transformTextWithTextView:self.contentView tickedPersonItems:currentSelectedPersonItems atAllPersons:nil canRepeat:NO needBack:self.needBack color:[UIColor colorWithHexString:@"#0099E6"] attributes:attributes completeBlock:^(NSMutableAttributedString *mutableAttributedString, NSRange selectedRange) {weakSelf.needBack = NO;weakSelf.contentView.attributedText = mutableAttributedString;weakSelf.contentView.selectedRange = selectedRange;[weakSelf textViewDidChange:weakSelf.contentView];}];}}//发送文案的时候,最终是从这里获取当前选中的人员
- (void)textViewDidChange:(UITextView *)textView {self.currentSelectedPersonItems = [NSMutableArray arrayWithArray:[self.contentView.attributedText getCurrentAtPersonItems]];DRLog(@"当前艾特的人%@",self.currentSelectedPersonItems);}- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{if ([text isEqualToString:@"@"]) {if (range.length > 0) {NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];[self.contentView.textStorage replaceCharactersInRange:range withAttributedString:attributedString];}self.needBack = YES; //需要将手动输入的@删除__weak typeof(self) weakSelf = self;dispatch_async(dispatch_get_main_queue(), ^{[weakSelf presentPersonListViewController];});return YES;}if ([text isEqualToString:@"\n"]) {[self sendClick];return NO;}return YES;
}
源文件已上传