自定义cell 各个空间的frame设置 QQ为例

本文详细阐述了消息模型中时间、头像、正文等元素的布局与解析过程,包括坐标定位、宽度计算及行高等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 -(void)setMessage:(GYLMessageModel *)message

{

    _message = message;

    

    //屏幕的宽度

    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

    //间隙

    CGFloat padding = 10;

    //时间

    CGFloat timeX = 0;

    CGFloat timeY = 0;

    CGFloat timeW = screenWidth;

    CGFloat timeH = 30;

    _timeF = CGRectMake(timeX, timeY, timeW, timeH);

    

    

    //2.头像

    CGFloat iconH = 30;

    CGFloat iconW = 30;

    CGFloat iconY = CGRectGetMaxY(_timeF) + padding;

    CGFloat iconX = 0;

    if (GYLMessageModelTypeMe == _message.type) {//自己发的

        //x = 屏幕宽度 - 间隙 - 头像宽度

        iconX = screenWidth - padding - iconW;

        

    }else iconX = padding;

    _iconF = CGRectMake(iconX, iconY, iconW, iconH);

    //3.正文

    NSDictionary * dict = @{NSFontAttributeName :GYLTextFont};

    CGSize maxSize = CGSizeMake(200MAXFLOAT);

    CGSize textSize = [_message.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;

    CGFloat textW = textSize.width;

    CGFloat textH = textSize.height;

    CGFloat textY = iconY;

    CGFloat textX = 0;

    if (GYLMessageModelTypeMe == _message.type) {//自己发的

        // x = 头像x - 间隙 - 文本的宽度

        textX = iconX - padding - textW;

    }else

    {

        //头像的最大的x+ 间隙

        textX = CGRectGetMidX(_iconF) + padding;

    }

    _textF = CGRectMake(textX, textY, textW, textH);

    

    // 4.行高

    CGFloat maxIconY = CGRectGetMaxY(_iconF);

    CGFloat maxTextY = CGRectGetMaxY(_textF);

    

    // _cellHeight = (maxIconY > maxTextY?  (maxIconY + padding) :  (maxIconY + padding));

    _cellHeight = MAX(maxIconY, maxTextY) + padding;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值