iOS重拾直播系列-直播聊天室

直播问题交流可加群 379258188 备注简书

此篇为重拾直播系列的第一篇,基于之前的项目聊天室进行了重构和优化!
优化点:1.使用自定义的融云消息类型 2.优化聊天室cell高度计算方式

当前项目效果展示:


直播间

此demo使用的是映客直播流,侵删

直播间的消息使用的是融云直播聊天室方案,使用了自定义消息类型

  • 文本消息(包括弹幕)
  • 点赞消息
  • 礼物消息
  • 特效消息(进场特效,全局礼物通知特效,GIF图中为展示)

在这里先把消息类型略过,在后面的文章中会介绍,本次在介绍聊天室的实现

直播聊天室的要求:

  • 富文本展示(等级+用户姓名+礼物缩略图)
  • 文本消息 (看我的大白眼进入了直播间,看我的大白眼关注了主播...)
  • 不卡顿(滚动流畅,高度计算准确)

cell高度计算的思考

配合Masonry实现TableViewCell的高度自适应,以及更易管理的高度缓存
这个文章的的方案思考比较深刻,再次我就不重复了!原本打算使用上述的方案中动态高度四:缓存高度,但是在使用过程了和设计需求有出入!不使用此方案还有一个重要原因,可以看我末尾补充。

聊天区域

背后的灰色是根据文本长度改变的

最终方案:
使用YYLabel中的YYTextLayout计算高度

    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(RCCRMsgW - 10.0f, MAXFLOAT)];
    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:self.messsageLabel.attributedText];
    CGSize __labelSize = textLayout.textBoundingSize;
   // 留出文本的上下间距增加15的高度
    return __labelSize.height + 15;

RCDLiveTextMessageCell.h

@interface RCDLiveTextMessageCell : UITableViewCell

@property (nonatomic, strong) RCCRMessageModel *model;

- (CGFloat)heightForModel:(RCCRMessageModel *)message;
@end

RCDLiveTextMessageCell.m

@interface RCDLiveTextMessageCell()

/*!
 显示消息内容的Label
 */
@property(nonatomic, strong) YYLabel *messsageLabel;

@property (nonatomic, strong) UIView *bgView;

@end

@implementation RCDLiveTextMessageCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.messsageLabel];
    }
    return self;
}

- (void)setModel:(RCCRMessageModel *)model {
    
    _model = model;
    // 进行数据赋值
    // 创建一个可变属性字符串
    NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc]init];
    UIFont *font = [UIFont systemFontOfSize:15];
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineSpacing = 3;
 
    UIImage *image = [UIImage imageNamed:@"dengji_1"]; // 测试数据
    NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
    [finalStr appendAttributedString:attachText];
    [finalStr appendAttributedString:[[NSAttributedString alloc] initWithString:model.name attributes:@{NSFontAttributeName : font,NSForegroundColorAttributeName:[UIColor whiteColor]}]];
    
    [finalStr appendAttributedString:[[NSAttributedString alloc] initWithString:model.message attributes:@{NSFontAttributeName : font,NSForegroundColorAttributeName:[UIColor yellowColor],NSParagraphStyleAttributeName:paragraphStyle}]];
    
    [finalStr addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, finalStr.length)];

    
    self.messsageLabel.attributedText = finalStr;
    
    // 很关键 宽度一定要准确 (250 是tableview的宽度,-10是让文本左右留出10的宽度)
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(250 - 10.0f, MAXFLOAT)];
    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:self.messsageLabel.attributedText];
    self.messsageLabel.textLayout = textLayout;
    
    self.messsageLabel.frame = CGRectMake(4,4, textLayout.textBoundingSize.width, textLayout.textBoundingSize.height);
    self.bgView.frame = CGRectMake(0, 0, self.messsageLabel.width + 8, self.messsageLabel.height + 6);
}


// 根绝数据计算cell的高度
- (CGFloat)heightForModel:(RCCRMessageModel *)message {
    
    [self setModel:message];
    // 很关键
    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(250 - 10.0f, MAXFLOAT)];
    YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:self.messsageLabel.attributedText];
    CGSize __labelSize = textLayout.textBoundingSize;
    return __labelSize.height + 15;
}

#pragma mark - lazy
- (YYLabel *)messsageLabel {
    
    if (_messsageLabel == nil) {
        _messsageLabel = [[YYLabel alloc]init];
        _messsageLabel.numberOfLines = 0;
        _messsageLabel.textAlignment = NSTextAlignmentLeft;
        _messsageLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _messsageLabel.displaysAsynchronously = YES;
        _messsageLabel.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    }
    return _messsageLabel;
}

- (UIView *)bgView {
    if (_bgView == nil) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.25];
        // 如果切原价卡顿的话,可以使用背景图
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

@end

高度计算

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RCCRMessageModel *model = [self.conversationDataRepository objectAtIndex:indexPath.row];
    if (model.cellHeight == 0) {
        
        CGFloat cellHeight = [self.tempMsgCell heightForModel:model];
        model.cellHeight = cellHeight;
        return cellHeight;
        
    }else{
        return model.cellHeight;
    }
}

本文demo https://github.com/TsuiOS/HsuLive

补充

// 根绝数据计算cell的高度
- (CGFloat)heightForModel:(CellModel *)message {
    [self setMessage:message];
    [self layoutIfNeeded];
    
    CGFloat cellHeight = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
    
    return cellHeight;
}

如果消息加载过快,cell的出现方式就会有问题:
GIF图不是特别明显,可以自己运行demo看效果


systemLayoutSizeFittingSize

使用YYTextLayout

YYTextLayout

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容