气泡聊天
来源:互联网 发布:网络借贷合法吗 编辑:程序博客网 时间:2024/03/29 22:01
#import "ViewController.h"
#import "ChatCell.h"
#import "ChatItem.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>{
NSMutableArray* _dataArray;
UITableView* _tableView;
UIView* _chatView;
UITextField* _textField;
}
@end
@implementation ViewController
//隐藏状态栏
- (BOOL)prefersStatusBarHidden{
return YES;
}
- (void)viewDidLoad {
[superviewDidLoad];
_dataArray = [[NSMutableArrayalloc] init];
_tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height -40)];
_tableView.delegate =self;
_tableView.dataSource =self;
//分割线
_tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
[self.viewaddSubview:_tableView];
_chatView = [[UIViewalloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-40,self.view.frame.size.width,40)];
_chatView.backgroundColor = [UIColorgrayColor];
[self.viewaddSubview:_chatView];
_textField = [[UITextFieldalloc] initWithFrame:CGRectMake(10,5, self.view.frame.size.width -70, 30)];
_textField.delegate =self;
_textField.borderStyle =UITextBorderStyleRoundedRect;
[_chatView addSubview:_textField];
UIButton* button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame =CGRectMake(self.view.frame.size.width -60, 5, 50, 30);
[button setTitle:@"发送"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(sendText)forControlEvents:UIControlEventTouchUpInside];
[_chatViewaddSubview:button];
//监听键盘出现
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
//监听键盘消失
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}
//键盘出现
- (void)keyboardWillShow:(NSNotification*)noti{
//键盘Size
CGSize size = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
_tableView.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height -40 - size.height);
_chatView.frame =CGRectMake(0,self.view.frame.size.height -40 - size.height,self.view.frame.size.width,40);
}
//收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//键盘将要消失
- (void)keyboardWillHide:(NSNotification*)noti{
_tableView.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height -40);
_chatView.frame =CGRectMake(0,self.view.frame.size.height -40, self.view.frame.size.width,40);
}
- (void)sendText{
ChatItem* item = [[ChatItemalloc] init];
item.isSelf =YES;
item.content =_textField.text;
_textField.text =@"";
[_dataArrayaddObject:item];
//刷新
NSIndexPath* indexPath = [NSIndexPathindexPathForRow:_dataArray.count-1inSection:0];
[_tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
//向上滚动
[_tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottomanimated:YES];
[NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(autoSpeak)userInfo:nilrepeats:NO];
}
//自动回复
- (void)autoSpeak{
//http://lqcjdx.blog.163.com/blog/static/20748924120138344325384/
NSArray* array =@[@"好的",@"没门",@"你想多了",@"我去",@"去死",@"一边去",@"没问题",@"妥妥的"];
ChatItem* item = [[ChatItemalloc] init];
item.isSelf =NO;
item.content = array[arc4random()%array.count];
[_dataArrayaddObject:item];
//刷新
NSIndexPath* indexPath = [NSIndexPathindexPathForRow:_dataArray.count-1inSection:0];
[_tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
//向上滚动
[_tableViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottomanimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return_dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ChatCell* cell = [tableViewdequeueReusableCellWithIdentifier:@"ID"];
if (cell == nil) {
cell = [[ChatCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"ID"];
//选中
cell.selectionStyle =UITableViewCellSelectionStyleNone;
}
ChatItem* item = _dataArray[indexPath.row];
//限制文本最大宽高
CGSize size = [item.contentboundingRectWithSize:CGSizeMake(250,1000) options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15.0]}context:nil].size;
if (item.isSelf) {
//我们自己发的
cell.leftView.hidden =YES;
cell.rightView.hidden =NO;
//显示内容
cell.rightLabel.text = item.content;
//显示大小
cell.rightLabel.frame =CGRectMake(10,5, size.width, size.height);
cell.rightView.frame =CGRectMake([[UIScreenmainScreen] bounds].size.width - size.width -30, 5, size.width +25, size.height +20);
}else {
//接收到的
cell.leftView.hidden =NO;
cell.rightView.hidden =YES;
cell.leftLabel.text = item.content;
cell.leftLabel.frame =CGRectMake(15,5, size.width, size.height);
cell.leftView.frame =CGRectMake(5,5, size.width +25, size.height +20);
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
ChatItem* item = _dataArray[indexPath.row];
//限制文本最大宽高
CGSize size = [item.contentboundingRectWithSize:CGSizeMake(250,1000) options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:15.0]}context:nil].size;
return size.height +20 + 10;
}
@end
//ChatItem
#import <Foundation/Foundation.h>
@interface ChatItem :NSObject
@property (nonatomic)BOOL isSelf;
@property (nonatomic,copy) NSString* content;
@end
#import <UIKit/UIKit.h>
@interface ChatCell :UITableViewCell
@property (nonatomic,strong) UIImageView* leftView;
@property (nonatomic,strong) UIImageView* rightView;
@property (nonatomic,strong) UILabel* leftLabel;
@property (nonatomic,strong) UILabel* rightLabel;
@end
#import "ChatCell.h"
@implementation ChatCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {
[selfmakeView];
}
return self;
}
- (void)makeView{
//气泡图片
UIImage* leftBubble = [UIImageimageNamed:@"ReceiverTextNodeBkg.png"];
UIImage* rightBubble = [UIImageimageNamed:@"SenderTextNodeBkg.png"];
//设定一行一列像素进行复制
leftBubble = [leftBubble stretchableImageWithLeftCapWidth:30topCapHeight:35];
rightBubble = [rightBubble stretchableImageWithLeftCapWidth:30topCapHeight:35];
//左气泡
self.leftView = [[UIImageViewalloc] initWithFrame:CGRectMake(5,5, 20,20)];
_leftView.image = leftBubble;
[self.contentViewaddSubview:_leftView];
//右气泡
self.rightView = [[UIImageViewalloc] initWithFrame:CGRectMake([[UIScreenmainScreen] bounds].size.width -25, 5, 20,20)];
_rightView.image = rightBubble;
[self.contentViewaddSubview:_rightView];
//左label
self.leftLabel = [[UILabelalloc] initWithFrame:CGRectMake(15,5, 5,5)];
_leftLabel.font = [UIFontsystemFontOfSize:15.0];
_leftLabel.numberOfLines =0;
[_leftView addSubview:_leftLabel];
//右label
self.rightLabel = [[UILabelalloc] initWithFrame:CGRectMake(10,5, 5,5)];
_rightLabel.font = [UIFontsystemFontOfSize:15.0];
_rightLabel.numberOfLines =0;
[_rightView addSubview:_rightLabel];
}
@end
- 气泡聊天
- 聊天气泡sms
- android聊天气泡
- 高仿微信气泡聊天
- iPhone 聊天气泡
- 聊天气泡效果实现
- iOS 气泡聊天效果
- 高仿微信气泡聊天
- APP气泡聊天框
- android实现气泡聊天
- QQ聊天气泡
- iOS制作聊天气泡
- 聊天对话气泡-字体自适应气泡宽度
- 仿微信聊天气泡效果实现
- 仿微信聊天气泡效果实现
- 仿微信聊天气泡效果实现
- Android仿微信气泡聊天界面设计
- Android仿微信气泡聊天界面设计
- 局势在恶化
- 从尾到头打印链表
- leetcode-53 Maximum Subarray 连续子数组的最大和
- MySQL 一对多查询,合并多的一方的信息
- Python 新式类与经典类的区别
- 气泡聊天
- 敢于尝试是人类成长前提
- Object类
- 数据预处理
- LinearLayout的border设置。
- HDU 5147 树状数组
- codeforces 412E E-mail Addresses
- C语言端口扫描
- Cookies学习