美文网首页
iOS textview中增加@人功能

iOS textview中增加@人功能

作者: money_ac9e | 来源:发表于2023-07-26 16:18 被阅读0次

textview中增加@人功能

第一步实现textView代理

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"@"]) {
        [self selectFriendVc];
        return NO;
    }
    
    if ([text isEqualToString:@""]) {
        return ![self deleteAtUser:range];
    }
    
    if ([text isEqualToString:@"\n"]) {
        [self dClickSendBtnActiond];
        return NO;
    }
    return YES;
}

第二步判断"@"符号,跳转

第三步实现删除功能

- (BOOL)deleteAtUser:(NSRange)range{
    // 因为光标在 rang中间也要删除  所以用for循环
    NNUserInfoModel *delemodel = [[NNUserInfoModel alloc]init];
    for (NNUserInfoModel *model in self.selectedFriend) {
        NSString *atName = [NSString stringWithFormat:@"@%@",model.userName];
        NSRange atRange = [self.inputTextView.text rangeOfString:atName];
        if (range.location>=atRange.location && range.location < atRange.location + atRange.length) {
            NSMutableString *mStr = [[NSMutableString alloc]initWithString:self.inputTextView.text];
            [mStr deleteCharactersInRange:atRange];
            delemodel = model;
            self.inputTextView.text = mStr;
            self.inputTextView.selectedRange = NSMakeRange(atRange.location, 0);//定位光标
            break;
        }
    }
    if (delemodel.uid > 0) {
        [self.selectedFriend removeObject:delemodel];
        [self textViewDidChange:self.inputTextView];
        return YES;
    }
    return NO;
}

第四步 内容改变刷新位置

-(void)textViewDidChange:(UITextView *)textView {
    
    UITextRange *markedTextRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:markedTextRange.start offset:0];
    
    if (position) {
        return;
    }
    
    NSRange selectedRange = textView.selectedRange;
    
    NSMutableAttributedString *attributedComment = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:14], NSForegroundColorAttributeName: pUIColorFromRGBp(0x242323)}];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5.0;
    [attributedComment addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedComment.length)];
    [self.selFriendRangeDict removeAllObjects];
    for (NNUserInfoModel *model in self.selectedFriend) {
        NSRange range = [textView.text rangeOfString:[NSString stringWithFormat:@"@%@",model.userName]];
        [self.selFriendRangeDict setValue:@{@"location":@(range.location),@"length":@(range.length), @"selectModel":model} forKey:[NSString stringWithFormat:@"%ld",(long)(range.location+range.length)]];
        
        [attributedComment addAttribute:NSForegroundColorAttributeName
                                  value:MainDistort
                                  range:range];
    }
    NSUInteger offset = textView.attributedText.length - attributedComment.length;
    textView.attributedText = attributedComment;
    textView.selectedRange = NSMakeRange(selectedRange.location - offset, 0);
    CGFloat height = textView.contentSize.height+20+7;
    if ([textView.text length] > scoopCommentLength) {
        self.inputTextView.text = [textView.text substringWithRange:NSMakeRange(0, scoopCommentLength)];
    }else{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height < 40 ? 40 : height);
        }];
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }];
    }
}

相关文章

网友评论

      本文标题:iOS textview中增加@人功能

      本文链接:https://www.haomeiwen.com/subject/mlmkpdtx.html