即时聊天 界面布局

//头像大小

#define HEAD_SIZE 45.0f

#define TEXT_MAX_HEIGHT 500.0f

//间距

#define INSETS 8.0f


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

   self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

    

   if (self) {

        // Initialization code

       //头像

        _userHead =[[CHAvatarViewalloc]initWithFrame:CGRectZero];

        [_userHeadsetBackgroundColor:[UIColorclearColor]];

        //聊天内容背景图片

        _bubbleBg =[[UIImageViewalloc]initWithFrame:CGRectZero];

        _showImage = [[UIImageViewalloc]initWithFrame:CGRectZero];

        [_showImagesetBackgroundColor:[UIColorclearColor]];

        _showImage.contentMode =UIViewContentModeScaleAspectFit;

       //聊天内容

        _messageConent=[[UILabelalloc]initWithFrame:CGRectZero];

        _headMask =[[UIImageViewalloc]initWithFrame:CGRectZero];

        _chatImage =[[UIImageViewalloc]initWithFrame:CGRectZero];

        

        [_messageConentsetBackgroundColor:[UIColorclearColor]];

        [_messageConentsetFont:[UIFontboldSystemFontOfSize:14]];

        _messageConent.textColor = [ToolkitcolorWithHexString:@"#333333"];

        [_messageConentsetNumberOfLines:20];

        _timeImg = [[UIImageViewalloc]initWithFrame:CGRectZero];

        [_timeImgsetImage:[UIImageimageNamed:@"time-gray"]];

       //聊天时间显示

       _timeLabel = [[UILabelalloc]initWithFrame:CGRectZero];

        [_timeLabelsetBackgroundColor:[UIColorclearColor]];

        [_timeLabelsetFont:[UIFontsystemFontOfSize:12]];

        [_timeLabelsetTextColor:[UIColorlightGrayColor]];

        [_timeLabelsetTextAlignment:NSTextAlignmentCenter];

        [self.contentViewaddSubview:_timeLabel];

        [self.contentViewaddSubview:_timeImg];

        [self.contentViewaddSubview:_bubbleBg];

        [self.contentViewaddSubview:_userHead];

        [selfaddSubview:_headMask];

        [self.contentViewaddSubview:_messageConent];

        [self.contentViewaddSubview:_chatImage];

       _showImage.hidden  =YES;

        [_bubbleBgaddSubview:_showImage];

        [_chatImagesetBackgroundColor:[UIColorredColor]];

        [selfsetSelectionStyle:UITableViewCellSelectionStyleNone];

        [_headMasksetImage:[[UIImageimageNamed:@"photo-size"]stretchableImageWithLeftCapWidth:10topCapHeight:10]];

        [selfsetBackgroundColor:[UIColorclearColor]];

    }

    return self;

}

-(void)layoutSubviews

{

    

    [superlayoutSubviews];

    

    

    

   NSString *orgin=_messageConent.text;

    CGSize textSize=[orginsizeWithFont:[UIFontboldSystemFontOfSize:15]constrainedToSize:CGSizeMake((320-HEAD_SIZE-3*INSETS-40),TEXT_MAX_HEIGHT)lineBreakMode:NSLineBreakByWordWrapping];


    

   switch (_msgStyle) {

        casekWCMessageCellStyleMe:

        {

            [_chatImagesetHidden:YES];

            [_messageConentsetHidden:NO];

            [_timeLabelsetFrame:CGRectMake(0,(CELL_HEIGHT-textSize.height)/2-35+7 ,kDeviceWidth ,20 )];

           _timeImg.frame =CGRectMake(79, (CELL_HEIGHT-textSize.height)/2-30+7,12, 12);

            [_messageConentsetFrame:CGRectMake(CELL_WIDTH-INSETS*2-HEAD_SIZE-textSize.width-15, (CELL_HEIGHT-textSize.height)/2+5, textSize.width, textSize.height)];

            [_userHeadsetFrame:CGRectMake(CELL_WIDTH-INSETS-HEAD_SIZE,INSETS+14,HEAD_SIZE ,HEAD_SIZE)];

            

             [_bubbleBgsetImage:[[UIImageimageNamed:@"SenderTextNodeBkg"]stretchableImageWithLeftCapWidth:20topCapHeight:30]];

           _bubbleBg.frame=CGRectMake(_messageConent.frame.origin.x-15,_messageConent.frame.origin.y-9, textSize.width+30, textSize.height+30);

           _showImage.frame =CGRectMake(15,5+2, 25, 25);

        }

           break;

        casekWCMessageCellStyleOther:

        {

            [_chatImagesetHidden:YES];

            [_messageConentsetHidden:NO];

            [_userHeadsetFrame:CGRectMake(INSETS,INSETS+14,HEAD_SIZE ,HEAD_SIZE)];

            [_timeLabelsetFrame:CGRectMake(0,(CELL_HEIGHT-textSize.height)/2-35+7 ,kDeviceWidth ,20 )];

           _timeImg.frame =CGRectMake(78, (CELL_HEIGHT-textSize.height)/2-30+7,12, 12);

            [_messageConentsetFrame:CGRectMake(2*INSETS+HEAD_SIZE+15, (CELL_HEIGHT-textSize.height)/2+5, textSize.width, textSize.height)];

            

            

            [_bubbleBgsetImage:[[UIImageimageNamed:@"ReceiverTextNodeBkg"]stretchableImageWithLeftCapWidth:20topCapHeight:30]];

           _bubbleBg.frame=CGRectMake(_messageConent.frame.origin.x-15,_messageConent.frame.origin.y-9, textSize.width+30, textSize.height+30);

           _showImage.frame =CGRectMake(15,5+2, 25, 25);

        }

           break;

       default:

           break;

    }

    

    

    _headMask.frame=CGRectMake(_userHead.frame.origin.x-1,_userHead.frame.origin.y-1,48, 48);

    

}

-(void)setMessageObject:(WCMessageObject*)aMessage

{

    [_messageConentsetText:aMessage.messageContent];

   

}



在cell中控制高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

   if( [[msgRecords[indexPath.row]messageType]intValue]==kWCMessageTypeImage)

      return 55+100+10;

  else{

    

   NSString *orgin=[msgRecords[indexPath.row]messageContent];

    CGSize textSize=[orginsizeWithFont:[UIFontboldSystemFontOfSize:15]constrainedToSize:CGSizeMake((320-HEAD_SIZE-3*INSETS-40),TEXT_MAX_HEIGHT)lineBreakMode:NSLineBreakByWordWrapping];

      return 55+textSize.height+10;}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值