气泡聊天 -- 类似短信、微信界面(计算输入框输入的内容的尺寸size)
2014-12-05 17:49
537 查看
//***********************************************************//
* 气泡聊天
//***********************************************************//
气泡聊天使用 : UITableview+UIView(放输入框)
保持气泡图片不失真的原理:是将一张规则的图片,选择它的一行一列的像素,根据内容大小对选择的这些像素进行复制。这样就可以保证,气泡图片不失真。
关键代码:
UIImage *leftImage=[UIImage imageNamed:@"ReceiverTextNodeBkg.png"];
UIImage *rightImage=[UIImage imageNamed:@"SenderTextNodeBkg.png"];
//设定一行一列像素进行复制,LeftCapWidth表示行,topCapHeight表是列,它们都是指的是:从图片的零零点位置开始算起的
leftImage=[leftImage stretchableImageWithLeftCapWidth:30 topCapHeight:40];
rightImage=[rightImage stretchableImageWithLeftCapWidth:30 topCapHeight:40];
因为点击输入框会弹出键盘。例如:微信,短信等,都是弹出键盘后,输入框自动向上移动。
//***********************************************************//
* 输入框,按下键盘,收起
//***********************************************************//
利用UIApplication的监听机制
//监听键盘出现
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
//监听键盘消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//键盘将要出现
-(void)keyboardWillAppear:(NSNotification *)noti
{
//获取键盘的size
CGSize size=[noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//改变tableview和view的frame
_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-45-size.height, self.view.frame.size.width, 45);
}
//键盘将要隐藏
-(void)keyboardWillHide:(NSNotification *)noti
{
//恢复tableview和view的frame
_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-45, self.view.frame.size.width, 45);
}
//***********************************************************//
* 计算输入框输入的内容的尺寸size
//***********************************************************//
关键代码:
//获取文本的size
CGSize size = [item.content boundingRectWithSize:CGSizeMake(250, 1000) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}
context:nil].size;
以下是工程代码:
1:工程文件目录截图:
2.工程代码:
2.1:数据model
AZChatItem:
2.2:视图View
AZChatTableViewCell:
2.3:主控制器Controller
AZViewController:
_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
3.模拟器效果:
* 气泡聊天
//***********************************************************//
气泡聊天使用 : UITableview+UIView(放输入框)
保持气泡图片不失真的原理:是将一张规则的图片,选择它的一行一列的像素,根据内容大小对选择的这些像素进行复制。这样就可以保证,气泡图片不失真。
关键代码:
UIImage *leftImage=[UIImage imageNamed:@"ReceiverTextNodeBkg.png"];
UIImage *rightImage=[UIImage imageNamed:@"SenderTextNodeBkg.png"];
//设定一行一列像素进行复制,LeftCapWidth表示行,topCapHeight表是列,它们都是指的是:从图片的零零点位置开始算起的
leftImage=[leftImage stretchableImageWithLeftCapWidth:30 topCapHeight:40];
rightImage=[rightImage stretchableImageWithLeftCapWidth:30 topCapHeight:40];
因为点击输入框会弹出键盘。例如:微信,短信等,都是弹出键盘后,输入框自动向上移动。
//***********************************************************//
* 输入框,按下键盘,收起
//***********************************************************//
利用UIApplication的监听机制
//监听键盘出现
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
//监听键盘消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//键盘将要出现
-(void)keyboardWillAppear:(NSNotification *)noti
{
//获取键盘的size
CGSize size=[noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//改变tableview和view的frame
_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-45-size.height, self.view.frame.size.width, 45);
}
//键盘将要隐藏
-(void)keyboardWillHide:(NSNotification *)noti
{
//恢复tableview和view的frame
_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-45, self.view.frame.size.width, 45);
}
//***********************************************************//
* 计算输入框输入的内容的尺寸size
//***********************************************************//
关键代码:
//获取文本的size
CGSize size = [item.content boundingRectWithSize:CGSizeMake(250, 1000) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}
context:nil].size;
以下是工程代码:
1:工程文件目录截图:
2.工程代码:
2.1:数据model
AZChatItem:
AZChatTableViewCell:
AZViewController:
相关文章推荐
- 实现类似QQ、微信聊天界面,标题栏固定,键盘不遮挡底部输入框
- iOS 类似微信,QQ聊天界面的气泡聊天简单实现Demo
- Android仿微信气泡聊天界面设计
- Lance老师UI系列教程第六课->微信聊天气泡界面的实现
- 类似qq微信的即时通讯聊天界面是如何定制的
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- Android仿微信气泡聊天界面设计
- iOS Dev (61) 文本输入框随键盘上移,类似信息、Whatsapp、微信中的聊天输入框
- 转-Android仿微信气泡聊天界面设计
- 解决类似微信聊天界面软键盘和表情框冲突问题
- ios-day08-01(模仿QQ聊天界面。ios通知的使用(对键盘状态的监听)、枚举和分类的使用、拉伸图片、计算文本字符串的尺寸
- Lance老师UI系列教程第六课->微信聊天气泡界面的实现
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
- Android仿微信气泡聊天界面设计
- Android仿微信气泡聊天界面设计
- ios 仿微信,短信聊天气泡
- ios 仿微信,短信聊天气泡