IOS聊天对话界面
大家好,百忙之中,抽出点空,写个微博,话说好久没写。
最近项目中有碰到写类似微信聊天界面上的效果,特整理了一下,写了一个小的Demo,希望给没头绪的同学们一个参考!
下载地址:http://files.cnblogs.com/ios8/WeixinDeom.zip
Demo下载地址:http://download.csdn.net/detail/rhljiayou/6524347
先看一下效果图:左图为截取微信的,右图是本demo的效果
再看一下主要代码实现:
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"weixin",@"name",@"微信团队欢迎你。很高兴你开启了微信生活,期待能为你和朋友们带来愉快的沟通体检。",@"content", nil];
- NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"hello",@"content", nil];
- NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"0",@"content", nil];
- NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"weixin",@"name",@"谢谢反馈,已收录。",@"content", nil];
- NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"0",@"content", nil];
- NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:@"weixin",@"name",@"谢谢反馈,已收录。",@"content", nil];
- NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试。",@"content", nil];
- _resultArray = [NSMutableArray arrayWithObjects:dict,dict1,dict2,dict3,dict4,dict5,dict6, nil];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- //泡泡文本
- - (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf withPosition:(int)position{
- //计算大小
- UIFont *font = [UIFont systemFontOfSize:14];
- CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
- // build single chat bubble cell with given text
- UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
- returnView.backgroundColor = [UIColor clearColor];
- //背影图片
- UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"SenderAppNodeBkg_HL":@"ReceiverTextNodeBkg" ofType:@"png"]];
- UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:floorf(bubble.size.width/2) topCapHeight:floorf(bubble.size.height/2)]];
- NSLog(@"%f,%f",size.width,size.height);
- //添加文本信息
- UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(fromSelf?15.0f:22.0f, 20.0f, size.width+10, size.height+10)];
- bubbleText.backgroundColor = [UIColor clearColor];
- bubbleText.font = font;
- bubbleText.numberOfLines = 0;
- bubbleText.lineBreakMode = NSLineBreakByWordWrapping;
- bubbleText.text = text;
- bubbleImageView.frame = CGRectMake(0.0f, 14.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+20.0f);
- if(fromSelf)
- returnView.frame = CGRectMake(320-position-(bubbleText.frame.size.width+30.0f), 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
- else
- returnView.frame = CGRectMake(position, 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
- [returnView addSubview:bubbleImageView];
- [returnView addSubview:bubbleText];
- return returnView;
- }
- //泡泡语音
- - (UIView *)yuyinView:(NSInteger)logntime from:(BOOL)fromSelf withIndexRow:(NSInteger)indexRow withPosition:(int)position{
- //根据语音长度
- int yuyinwidth = 66+fromSelf;
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.tag = indexRow;
- if(fromSelf)
- button.frame =CGRectMake(320-position-yuyinwidth, 10, yuyinwidth, 54);
- else
- button.frame =CGRectMake(position, 10, yuyinwidth, 54);
- //image偏移量
- UIEdgeInsets imageInsert;
- imageInsert.top = -10;
- imageInsert.left = fromSelf?button.frame.size.width/3:-button.frame.size.width/3;
- button.imageEdgeInsets = imageInsert;
- [button setImage:[UIImage imageNamed:fromSelf?@"SenderVoiceNodePlaying":@"ReceiverVoiceNodePlaying"] forState:UIControlStateNormal];
- UIImage *backgroundImage = [UIImage imageNamed:fromSelf?@"SenderVoiceNodeDownloading":@"ReceiverVoiceNodeDownloading"];
- backgroundImage = [backgroundImage stretchableImageWithLeftCapWidth:20 topCapHeight:0];
- [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
- UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(fromSelf?-30:button.frame.size.width, 0, 30, button.frame.size.height)];
- label.text = [NSString stringWithFormat:@"%d''",logntime];
- label.textColor = [UIColor grayColor];
- label.font = [UIFont systemFontOfSize:13];
- label.textAlignment = NSTextAlignmentCenter;
- label.backgroundColor = [UIColor clearColor];
- [button addSubview:label];
- return button;
- }
- #pragma UITableView
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return _resultArray.count;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSDictionary *dict = [_resultArray objectAtIndex:indexPath.row];
- UIFont *font = [UIFont systemFontOfSize:14];
- CGSize size = [[dict objectForKey:@"content"] sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
- return size.height+44;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }else{
- for (UIView *cellView in cell.subviews){
- [cellView removeFromSuperview];
- }
- }
- NSDictionary *dict = [_resultArray objectAtIndex:indexPath.row];
- //创建头像
- UIImageView *photo ;
- if ([[dict objectForKey:@"name"]isEqualToString:@"rhl"]) {
- photo = [[UIImageView alloc]initWithFrame:CGRectMake(320-60, 10, 50, 50)];
- [cell addSubview:photo];
- photo.image = [UIImage imageNamed:@"photo1"];
- if ([[dict objectForKey:@"content"] isEqualToString:@"0"]) {
- [cell addSubview:[self yuyinView:1 from:YES withIndexRow:indexPath.row withPosition:65]];
- }else{
- [cell addSubview:[self bubbleView:[dict objectForKey:@"content"] from:YES withPosition:65]];
- }
- }else{
- photo = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
- [cell addSubview:photo];
- photo.image = [UIImage imageNamed:@"photo"];
- if ([[dict objectForKey:@"content"] isEqualToString:@"0"]) {
- [cell addSubview:[self yuyinView:1 from:NO withIndexRow:indexPath.row withPosition:65]];
- }else{
- [cell addSubview:[self bubbleView:[dict objectForKey:@"content"] from:NO withPosition:65]];
- }
- }
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- }
- @end
其实很简单,主要说一下两个知识点,重要的两个知识点就能写出完美的泡泡对话聊天!
第一个是NSString中的一个方法:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode;
根据文本内容,算出所需要的大小CGSize;
第二个是UIImage中的一个方法:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
这里有几遍文章供大家参考这个方法的使用:
http://blog.csdn.net/lixing333/article/details/7589281
http://blog.csdn.net/w122079514/article/details/7848980
http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html
ok!完美,perfect!
IOS聊天对话界面的更多相关文章
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
原文链接 : Create an iOS Chat App using JSQMessagesViewController – Part 2 原文作者 : Mariusz Wisniewski 译者 ...
- IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
大家好,百忙之中,抽出点空,写个微博,话说好久没写. 最近项目中有碰到写类似微信聊天界面上的效果,特整理了一下,写了一个小的Demo,希望给没头绪的同学们一个参考! 下载地址:http://files ...
- [iPhone高级] 基于XMPP的IOS聊天客户端程序(IOS端一)
介绍完了服务器,这篇我们就要介绍重点了,写我们自己的IOS客户端程序 先看一下我们完成的效果图 首先下载xmppframework这个框架,下载 点ZIP下载 接下来,用Xcode新建一个工程 将以下 ...
- iOS聊天下拉刷新聊天记录的实现
1. 想法 最近在开发一个社交类app,要实现类似微信那种下拉刷新聊天记录的功能. 一般有两种实现方式: 1. 直接fetch一个entity的所有数据然后在内存中做filter,就是把所有聊天记录先 ...
- 基于XMPP的IOS聊天客户端程序
简介:XMPP协议是一种基于Socket长连接.以XML格式进行基本信息交换.C/S S/S多种架构的聊天协议 XMPPServer 基于XMPP协议的服务端(例如eJabber.OpenFire) ...
- [XMPP]iOS聊天软件学习笔记[四]
昨天完成了聊天界面,基本功能算告一段落 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework git clone https://githu ...
- iOS 聊天界面
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- ios 聊天demo 和nsoperationdemo
http://blog.csdn.net/zhibudefeng/article/details/7991649 http://blog.csdn.net/kangx6/article/details ...
- [XMPP]iOS聊天软件学习笔记[三]
今天做了好友界面,其实xmpp内部已经写好很多扩展模块,所以使用起来还是很方便的 开发时间:五天(工作时间) 开发工具:xcode6 开发平台:iOS8 XMPP框架:XMPPFramework gi ...
随机推荐
- left join与on,where 结合一起用的异同
I.数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left join时,on和where条件的区别如下: 1. on条件是在生成临时表时使用 ...
- Innodb中的事务隔离级别和锁的关系
前言: 我们都知道事务的几种性质,数据库为了维护这些性质,尤其是一致性和隔离性,一般使用加锁这种方式.同时数据库又是个高并发的应用,同一时间会有大量的并发访问,如果加锁过度,会极大的降低并发处理能力. ...
- JavaWeb基础: 第一个Web应用(Servlet)
Servlet的生命周期 <servlet-mapping>和<servlet> Web应用的用户是通过指定浏览器中URL地址来访问Web应用提供的静态或者是动态资源,如果Se ...
- Spring配置文件解析--bean属性
1.bean设置别名,多个别名用逗号隔开 <!--使用alias--> <bean id="app:dataSource" class="...&quo ...
- XX宝面试题——css部分
1.<b></b>与<strong></strong>有什么不同? 1) <b>标签是一个实体标签,它所包围的字符将被设为bold(粗体), ...
- 5 echo展开
一.(字符)展开 1.echo在标准输出中打印出他的文本 echo this is a test 输出 this is a test 2.输出通配符时,比如*代表匹配文件名的任何字符 echo * 输 ...
- java下载网络图片
import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IO ...
- jquery $post $get $
Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了. 推荐一篇不错的jQuery Ajax 实例文章,忘记了可以去看看,地址为:ht ...
- MySQL的information_schema的介绍
information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式.什么是元数据呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等.有些时候用于表述该信 ...
- 使用Camera进行拍照
Android应用提供了Camera来控制拍照,使用Camera进行拍照的步骤如下: 1.调用Camera的open()方法打开相机. 2.调用Camera的getParameters()方法获取拍照 ...