高仿微信和qq气泡聊天(整合精简版)

本文介绍如何在iOS应用中自定义消息单元格(cell),包括设置背景图片、文本标签及图片视图,并根据不同消息来源调整布局。
//定制单元cell
#import "TableViewCell.h"

@implementation TableViewCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //计算大小
        self.backgroundColor = [UIColor clearColor];
        _imgView = [[UIImageView alloc] init ];
        //添加文本信息
       _txtLabel = [[UILabel alloc] init ];
        _txtLabel.backgroundColor = [UIColor clearColor];
         UIFont *font = [UIFont systemFontOfSize:14];
        _txtLabel.font = font;
        _txtLabel.numberOfLines = 0;
        _txtLabel.lineBreakMode = NSLineBreakByWordWrapping;


        _photo = [[UIImageView alloc]init ];

        [self addSubview:_photo];
        [self addSubview:_imgView];
        [self addSubview:_txtLabel];
    }

    return self;
}
//在tableview 这样做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"msg_cell"];
    if (!cell) {
        cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"msg_cell"];
    }

    SZLMessage *msg=(SZLMessage *)_msgs[indexPath.row];
     BOOL fromSelf=YES;
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [msg.msg sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];


    UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"SenderAppNodeBkg_HL":@"ReceiverTextNodeBkg" ofType:@"png"]];
    cell.imgView.image=img;
    cell.photo.image = [UIImage imageNamed:@"16.png"];
    cell.txtLabel.text=msg.msg;
    float width=[UIScreen mainScreen].bounds.size.width-60;
    if(fromSelf){
        cell.txtLabel.frame=CGRectMake(width-(cell.txtLabel.frame.size.width+20.0f), 20.0f, size.width+10, size.height+10);
        cell.imgView.frame = CGRectMake(width-20-(cell.txtLabel.frame.size.width+20.0f), 14.0f, cell.txtLabel.frame.size.width+30.0f, cell.txtLabel.frame.size.height+20.0f);
        cell.photo.frame= CGRectMake(width, 10, 50, 50);
    }else{
        cell.txtLabel.frame=CGRectMake(85, 20.0f, size.width+10, size.height+10);
        cell.imgView.frame = CGRectMake(65, 14.0f, cell.txtLabel.frame.size.width+30.0f, cell.txtLabel.frame.size.height+20.0f);
        cell.photo.frame= CGRectMake(10, 10, 50, 50);
    }
    return cell;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值