美文网首页
融云聊天“已读”功能界面自定义

融云聊天“已读”功能界面自定义

作者: sj1910 | 来源:发表于2019-11-21 17:20 被阅读0次

首先设置开通“已读”功能
如果是不同APP之间聊天,都要开通“已读功能”

[RCIM sharedRCIM].enabledReadReceiptConversationTypeList =
    @[@(ConversationType_PRIVATE)];

*继承RCConversationViewController,重写- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath )indexPath
需要注意的一点的是,重写cell方法后,有网址和电话号码时,字体颜色不一样时,会出现cell复用问题,

- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    
    RCMessageModel *model = [self.conversationDataRepository objectAtIndex:indexPath.row];
    
    if (([cell isKindOfClass:[RCTextMessageCell class]] || [cell isKindOfClass:[RCImageMessageCell class]] || [cell isKindOfClass:[RCVoiceMessageCell class]] || [cell isKindOfClass:[RCLocationMessageCell class]]) && model.conversationType == ConversationType_PRIVATE) {
        if (model.content) {
            for (UIView *view in [((RCMessageCell *)cell).messageHasReadStatusView subviews]) {
                [view removeFromSuperview];
            }
            UILabel *hasReadView = [[UILabel alloc] init];
            hasReadView.textAlignment = NSTextAlignmentRight;
            hasReadView.font = [UIFont systemFontOfSize:12];
            hasReadView.textColor = RGBOF(0x666666);
            ((RCMessageCell *)cell).messageHasReadStatusView.hidden = NO;
            if (model.sentStatus == SentStatus_READ) {
                hasReadView.text = @"已读";
            } else if (model.sentStatus == SentStatus_SENT) {
                hasReadView.text = @"未读";
            }
            [((RCMessageCell *)cell).messageHasReadStatusView addSubview:hasReadView];
            [hasReadView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(((RCMessageCell *)cell).messageContentView.mas_left).offset(-AutoSizeScaleX(5));
                make.bottom.equalTo(((RCMessageCell *)cell).messageContentView.mas_bottom).offset(-AutoSizeScaleY(5));
            }];
        } else {
            if ([cell isKindOfClass:[RCTextMessageCell class]] || [cell isKindOfClass:[RCImageMessageCell class]] || [cell isKindOfClass:[RCVoiceMessageCell class]] || [cell isKindOfClass:[RCLocationMessageCell class]]) {
                for (UIView *view in [((RCMessageCell *)cell).messageHasReadStatusView subviews]) {
                    [view removeFromSuperview];
                }
            }
            
        }
    } else {
        NSLog(@"%@",[cell class]);
    }
    //解决cell复用字体颜色问题
    if ([cell isKindOfClass:[RCTextMessageCell class]]){
        
        //改变字体颜色
        RCTextMessageCell *textCell = (RCTextMessageCell *)cell;
        textCell.textLabel.textColor = RGBOF(0x000000);
        NSError *error;
        NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber | NSTextCheckingTypeLink error:&error];
        NSArray *matches = [detector matchesInString:textCell.textLabel.text options:0 range:NSMakeRange(0, [textCell.textLabel.text length])];

        for(NSTextCheckingResult*match in matches) {
             NSString *substringForMatch = [textCell.textLabel.text substringWithRange:match.range];
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textCell.textLabel.text];
            
            if ([match resultType] == NSTextCheckingTypeLink) {
                
                [attributedString addAttribute:NSForegroundColorAttributeName value:RGBOF(0x66cdaa) range:[textCell.textLabel.text rangeOfString:substringForMatch]];
                }
            
            if ([match resultType] == NSTextCheckingTypePhoneNumber) {
                
                [attributedString addAttribute:NSForegroundColorAttributeName value:RGBOF(0x1e90ff) range:[textCell.textLabel.text rangeOfString:substringForMatch]];
//                [attributedStringaddAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:matchRange];
                
            }
            
            textCell.textLabel.attributedText = attributedString;
    
        }
    }
}

- (void)messageCellUpdateSendingStatusEvent:(NSNotification *)notification {
    RCMessageCellNotificationModel *notifyModel = notification.object;
    if (notifyModel && ![notifyModel.actionName isEqualToString:CONVERSATION_CELL_STATUS_SEND_PROGRESS]) {
        [self reloadMessageCell:notifyModel.messageId];
    }
}

- (void)reloadMessageCell:(long)messageId {
    __weak typeof(self) __weakself = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0; i < __weakself.conversationDataRepository.count; i++) {
            RCMessageModel *model = (__weakself.conversationDataRepository)[i];
            if (messageId == model.messageId) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
                [__weakself hideCellReceiptView:indexPath withMessageModel:model];
                break;
            }
        }
    });
}

- (void)hideCellReceiptView:(NSIndexPath *)indexPath withMessageModel:(RCMessageModel *)model {
    
    UICollectionViewCell *__cell = [self.conversationMessageCollectionView cellForItemAtIndexPath:indexPath];
    
    [self.conversationMessageCollectionView reloadItemsAtIndexPaths:@[ indexPath ]];
    
    //如果是空说明被回收了,重新dequeue一个cell,这里用来解决已读人数闪的问题
    
    if (__cell) {
        
        if ([__cell isKindOfClass:[RCMessageCell class]]) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                ((RCMessageCell *)__cell).receiptCountLabel.hidden = YES;
                
            });
            
        }
        
    }
    
}

相关文章

网友评论

      本文标题:融云聊天“已读”功能界面自定义

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