IOS即时聊天UI设计

//消息实体类ChatMessage.h

#import <Foundation/Foundation.h>


@interface ChatMessage :NSObject

@property (nonatomic,strong) NSString *content;

@property (nonatomic,assign) BOOL isSelfSend;


@end

//实现类

#import "ChatMessage.h"


@implementation ChatMessage


@end


//自定义cell

#import <UIKit/UIKit.h>


@interface ChatMainViewCell :UITableViewCell


@property (nonatomic,strong) UIImageView *leftImageView;

@property (nonatomic,strong) UIImageView *rightImageView;

@property (nonatomic,strong) UILabel *leftLabel;

@property (nonatomic,strong) UILabel *rightLabel;


@end

// ChatMainViewCell.m文件

#import "ChatMainViewCell.h"


@implementation ChatMainViewCell


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

{

   self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];

   if (self) {

        [selfmakeView];

    }

    return self;

}


- (void)makeView{

    UIImage *leftImage=[UIImageimageNamed:@"ReceiverTextNodeBkg.png"];

    UIImage *rightImage=[UIImageimageNamed:@"SenderTextNodeBkg.png"];

    //获取图片某像素进行拉伸

    leftImage=[leftImage stretchableImageWithLeftCapWidth:30topCapHeight:35];

    rightImage=[rightImage stretchableImageWithLeftCapWidth:30topCapHeight:35];

    

    self.leftImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(10,5, 66,54)];

   self.leftImageView.image=leftImage;

    [self.contentViewaddSubview:self.leftImageView];

    //leftLabel

    self.leftLabel=[[UILabelalloc]initWithFrame:CGRectMake(15,5, 10,10)];

    self.leftLabel.font=[UIFontsystemFontOfSize:14];

    self.leftLabel.backgroundColor=[UIColorclearColor];

    self.leftLabel.numberOfLines=0;

    //字符折行

    self.leftLabel.lineBreakMode=NSLineBreakByCharWrapping;

    [self.leftImageViewaddSubview:self.leftLabel];

   //右侧

    self.rightImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(320-76,5, 66,54)];

   self.rightImageView.image=rightImage;

    [self.contentViewaddSubview:self.rightImageView];

    self.rightLabel=[[UILabelalloc]initWithFrame:CGRectMake(15,5, 10,10)];

    self.rightLabel.font=[UIFontsystemFontOfSize:14];

    self.rightLabel.backgroundColor=[UIColorclearColor];

    self.rightLabel.numberOfLines=0;

    self.rightLabel.lineBreakMode=NSLineBreakByCharWrapping;

    [self.rightImageViewaddSubview:self.rightLabel];

    

}

@end

//viewController.h

#import <UIKit/UIKit.h>


@interface ViewController :UIViewController<UITableViewDataSource ,

                            UITableViewDelegate ,UITextFieldDelegate>

{

   UITableView *_tableView;

   NSMutableArray *_dataArray;

   UIView *_chatView;

   UITextField *_textField;

}

@end

//viewController.m


#import "ViewController.h"

#import "ChatMessage.h"

#import "ChatMainViewCell.h"

@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

_dataArray=[NSMutableArrayarray];

    _tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,20, 320, 420)];

    _tableView.delegate=self;

    _tableView.dataSource=self;

    //隐藏表格的分割线

    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;

    [self.viewaddSubview:_tableView];

    //聊天面板

    _chatView=[[UIViewalloc]initWithFrame:CGRectMake(0,420, 320, 40)];

    _chatView.backgroundColor=[UIColorgrayColor];

    [self.viewaddSubview:_chatView];

    //输入信息框

    _textField=[[UITextFieldalloc]initWithFrame:CGRectMake(10,5, 200,30)];

    _textField.borderStyle=UITextBorderStyleRoundedRect;

    _textField.delegate=self;

    [_chatView addSubview:_textField];

    //发送按钮

    UIButton *send=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    send.frame=CGRectMake(250,5, 50, 30);

    [send setTitle:@"发送"forState:UIControlStateNormal];

    [send addTarget:selfaction:@selector(sendMessage)forControlEvents:UIControlEventTouchUpInside];

    [_chatViewaddSubview:send];

    //键盘出现的监听

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

    //键盘收起的监听

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

}


- (void)keyboardWillShow:(NSNotification *)noti{

    CGSize size=[[noti.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue ].size;

    [UIViewanimateWithDuration:0.25animations:^{

       _tableView.frame=CGRectMake(0,20, 320, 420-size.height);

       _chatView.frame=CGRectMake(0,440-size.height,320, 40);

        

    }];

}


- (void)keyboardWillHide:(NSNotification *)noti{

   _tableView.frame=CGRectMake(0,20, 320, 420);

   _chatView.frame=CGRectMake(0,440, 320, 40);

}

//键盘收起

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [_textFieldresignFirstResponder];

    return YES;


}


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

   ChatMessage *chat=[_dataArrayobjectAtIndex:indexPath.row];

    //计算气泡的拉伸大小

    CGSize size=[chat.contentsizeWithFont:[UIFontsystemFontOfSize:14.0f]constrainedToSize:CGSizeMake(250,1000) lineBreakMode:NSLineBreakByCharWrapping];

   return size.height+30;


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return_dataArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ChatMainViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:@"ID"];

   if (cell==nil) {

        cell=[[ChatMainViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"ID"];

        //隐藏cell被选定

        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }

   ChatMessage *chat=[_dataArrayobjectAtIndex:indexPath.row];

    //计算气泡的拉伸大小

    CGSize size=[chat.contentsizeWithFont:[UIFontsystemFontOfSize:14.0f]constrainedToSize:CGSizeMake(250,1000) lineBreakMode:NSLineBreakByCharWrapping];

    

    //自己发送的消息

   if (chat.isSelfSend) {

        cell.leftImageView.hidden=YES;

        cell.rightImageView.hidden=NO;

        cell.rightLabel.text=chat.content;

        cell.rightLabel.frame=CGRectMake(10,5, size.width, size.height);

        //计算气泡的拉伸

        cell.rightImageView.frame=CGRectMake(320-10-size.width-30,5, 30+size.width,20+size.height);

        

        

    }else{

    //别人发送的

        cell.rightImageView.hidden=YES;

        cell.leftImageView.hidden=NO;

        cell.leftLabel.text=chat.content;

        cell.leftLabel.frame=CGRectMake(15,5, size.width, size.height);

        cell.leftImageView.frame=CGRectMake(10,5, 30+size.width,20+size.height);

    }

   return cell;

}

//发送消息

- (void)sendMessage{

    ChatMessage *message=[[ChatMessagealloc]init];

    message.isSelfSend=YES;

    message.content=_textField.text;

    [_dataArrayaddObject:message];

    //清空输入框的内容

    _textField.text=@"";

    //插入表格中

    NSIndexPath *indexPath=[NSIndexPathindexPathForRow:_dataArray.count-1inSection:0];

    [_tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [_tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottomanimated:YES];

    //模拟别人发送消息

    [NSTimerscheduledTimerWithTimeInterval:2.0target:selfselector:@selector(autoSendMessage)userInfo:nilrepeats:NO];


}

//自动发送消息

- (void)autoSendMessage{

   NSArray *array=[NSArrayarrayWithObjects:@"星期三",@"天气晴",@"吃的鸡腿",nil];

   int num=arc4random()%array.count;

    ChatMessage *chat=[[ChatMessagealloc]init];

    chat.isSelfSend=NO;

    chat.content=[arrayobjectAtIndex:num];

    [_dataArrayaddObject:chat];

    NSIndexPath *indexPath=[NSIndexPathindexPathForRow:_dataArray.count-1inSection:0];

    [_tableViewinsertRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [_tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottomanimated:YES];


}


@end


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ChatKit 是一个免费且开源的 UI 聊天组件,自带云服务器,自带推送,支持消息漫游,消息永久存储。底层聊天服务基于LeanCloud(原名 AVOS ) 的 IM 实时通信服务「LeanMessage」而开发,采用 Protobuf 协议进行消息传输。ChatKit 可以帮助开发者快速集成 IM 服务,轻松实现聊天功能,提供完全自由的授权协议,支持二次开发。其最大特点是把聊天常用的一些功能配合 UI 一起提供给开发者。运行效果:示例代码:由最近联系人进入聊天界面按照上面的步骤,我们可以非常方便地打开最近联系人页面。但是我们会发现,点击其中的某个联系人/聊天群组,我们并不能直接进入聊天界面。要做到这一点,我们需要给 LCChatKit 设置上事件响应函数,示例代码如下:[[LCChatKit sharedInstance] setDidSelectConversationsListCellBlock:^(NSIndexPath *indexPath, AVIMConversation *conversation, LCCKConversationListViewController *controller) {     NSLog(@"conversation selected");     LCCKConversationViewController *conversationVC = [[LCCKConversationViewController alloc] initWithConversationId:conversation.conversationId];     [controller.navigationController pushViewController:conversationVC animated:YES]; }];对于联系人列表页面,我们在 LCChatKit 可以响应如下四种操作:/*!  *  选中某个对话后的回调 (比较常见的需求)  *  @param conversation 被选中的对话  */ typedef void(^LCCKConversationsListDidSelectItemBlock)(NSIndexPath *indexPath, AVIMConversation *conversation, LCCKConversationListViewController *controller); /*!  *  设置选中某个对话后的回调  */ - (void)setDidSelectConversationsListCellBlock:(LCCKConversationsListDidSelectItemBlock)didSelectItemBlock; /*!  *  删除某个对话后的回调 (一般不需要做处理)  *  @param conversation 被选中的对话  */ typedef void(^LCCKConversationsListDidDeleteItemBlock)(NSIndexPath *indexPath, AVIMConversation *conversation, LCCKConversationListViewController *controller); /*!  *  设置删除某个对话后的回调  */ - (void)setDidDeleteConversationsListCellBlock:(LCCKConversationsListDidDeleteItemBlock)didDeleteItemBlock; /*!  *  对话左滑菜单设置block (最近联系人页面有复杂的手势操作时,可以通过这里扩展实现)  *  @return  需要显示的菜单数组  *  @param conversation, 对话  *  @param editActions, 默认的菜单数组,成员为 UITableViewRowAction 类型  */ typedef NSArray *(^LCCKConversationEditActionsBlock)(NSIndexPath *indexPath, NSArray<UITableViewRowAction *> *editActions, AVIMConversation *conversation, LCCKConversationListViewController *controller); /*!  *  可以通过这个block设置对话列表中每个对话的左滑菜单,这个是同步调用的,需要尽快返回,否则会卡住UI  */ - (void)setConversationEditActionBlock:(LCCKConversationEditActionsBlock)conversationEditActionBlock; 标签:ChatKit

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值