大家好,百忙之中,抽出点空,写个微博,话说好久没写。

最近项目中有碰到写类似微信聊天界面上的效果,特整理了一下,写了一个小的Demo,希望给没头绪的同学们一个参考!

下载地址:http://files.cnblogs.com/ios8/WeixinDeom.zip

Demo下载地址:http://download.csdn.net/detail/rhljiayou/6524347

先看一下效果图:左图为截取微信的,右图是本demo的效果

     

再看一下主要代码实现:

  1. @implementation ViewController
  2. - (void)viewDidLoad
  3. {
  4. [super viewDidLoad];
  5. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"weixin",@"name",@"微信团队欢迎你。很高兴你开启了微信生活,期待能为你和朋友们带来愉快的沟通体检。",@"content", nil];
  6. NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"hello",@"content", nil];
  7. NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"0",@"content", nil];
  8. NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"weixin",@"name",@"谢谢反馈,已收录。",@"content", nil];
  9. NSDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"0",@"content", nil];
  10. NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:@"weixin",@"name",@"谢谢反馈,已收录。",@"content", nil];
  11. NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:@"rhl",@"name",@"大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试,大数据测试,长数据测试。",@"content", nil];
  12. _resultArray = [NSMutableArray arrayWithObjects:dict,dict1,dict2,dict3,dict4,dict5,dict6, nil];
  13. }
  14. - (void)didReceiveMemoryWarning
  15. {
  16. [super didReceiveMemoryWarning];
  17. // Dispose of any resources that can be recreated.
  18. }
  19. //泡泡文本
  20. - (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf withPosition:(int)position{
  21. //计算大小
  22. UIFont *font = [UIFont systemFontOfSize:14];
  23. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
  24. // build single chat bubble cell with given text
  25. UIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];
  26. returnView.backgroundColor = [UIColor clearColor];
  27. //背影图片
  28. UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"SenderAppNodeBkg_HL":@"ReceiverTextNodeBkg" ofType:@"png"]];
  29. UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:floorf(bubble.size.width/2) topCapHeight:floorf(bubble.size.height/2)]];
  30. NSLog(@"%f,%f",size.width,size.height);
  31. //添加文本信息
  32. UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(fromSelf?15.0f:22.0f, 20.0f, size.width+10, size.height+10)];
  33. bubbleText.backgroundColor = [UIColor clearColor];
  34. bubbleText.font = font;
  35. bubbleText.numberOfLines = 0;
  36. bubbleText.lineBreakMode = NSLineBreakByWordWrapping;
  37. bubbleText.text = text;
  38. bubbleImageView.frame = CGRectMake(0.0f, 14.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+20.0f);
  39. if(fromSelf)
  40. 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);
  41. else
  42. returnView.frame = CGRectMake(position, 0.0f, bubbleText.frame.size.width+30.0f, bubbleText.frame.size.height+30.0f);
  43. [returnView addSubview:bubbleImageView];
  44. [returnView addSubview:bubbleText];
  45. return returnView;
  46. }
  47. //泡泡语音
  48. - (UIView *)yuyinView:(NSInteger)logntime from:(BOOL)fromSelf withIndexRow:(NSInteger)indexRow  withPosition:(int)position{
  49. //根据语音长度
  50. int yuyinwidth = 66+fromSelf;
  51. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  52. button.tag = indexRow;
  53. if(fromSelf)
  54. button.frame =CGRectMake(320-position-yuyinwidth, 10, yuyinwidth, 54);
  55. else
  56. button.frame =CGRectMake(position, 10, yuyinwidth, 54);
  57. //image偏移量
  58. UIEdgeInsets imageInsert;
  59. imageInsert.top = -10;
  60. imageInsert.left = fromSelf?button.frame.size.width/3:-button.frame.size.width/3;
  61. button.imageEdgeInsets = imageInsert;
  62. [button setImage:[UIImage imageNamed:fromSelf?@"SenderVoiceNodePlaying":@"ReceiverVoiceNodePlaying"] forState:UIControlStateNormal];
  63. UIImage *backgroundImage = [UIImage imageNamed:fromSelf?@"SenderVoiceNodeDownloading":@"ReceiverVoiceNodeDownloading"];
  64. backgroundImage = [backgroundImage stretchableImageWithLeftCapWidth:20 topCapHeight:0];
  65. [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
  66. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(fromSelf?-30:button.frame.size.width, 0, 30, button.frame.size.height)];
  67. label.text = [NSString stringWithFormat:@"%d''",logntime];
  68. label.textColor = [UIColor grayColor];
  69. label.font = [UIFont systemFontOfSize:13];
  70. label.textAlignment = NSTextAlignmentCenter;
  71. label.backgroundColor = [UIColor clearColor];
  72. [button addSubview:label];
  73. return button;
  74. }
  75. #pragma UITableView
  76. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  77. {
  78. return 1;
  79. }
  80. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  81. {
  82. return _resultArray.count;
  83. }
  84. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  85. {
  86. NSDictionary *dict = [_resultArray objectAtIndex:indexPath.row];
  87. UIFont *font = [UIFont systemFontOfSize:14];
  88. CGSize size = [[dict objectForKey:@"content"] sizeWithFont:font constrainedToSize:CGSizeMake(180.0f, 20000.0f) lineBreakMode:NSLineBreakByWordWrapping];
  89. return size.height+44;
  90. }
  91. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. static NSString *CellIdentifier = @"Cell";
  94. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  95. if (cell == nil) {
  96. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  97. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  98. }else{
  99. for (UIView *cellView in cell.subviews){
  100. [cellView removeFromSuperview];
  101. }
  102. }
  103. NSDictionary *dict = [_resultArray objectAtIndex:indexPath.row];
  104. //创建头像
  105. UIImageView *photo ;
  106. if ([[dict objectForKey:@"name"]isEqualToString:@"rhl"]) {
  107. photo = [[UIImageView alloc]initWithFrame:CGRectMake(320-60, 10, 50, 50)];
  108. [cell addSubview:photo];
  109. photo.image = [UIImage imageNamed:@"photo1"];
  110. if ([[dict objectForKey:@"content"] isEqualToString:@"0"]) {
  111. [cell addSubview:[self yuyinView:1 from:YES withIndexRow:indexPath.row withPosition:65]];
  112. }else{
  113. [cell addSubview:[self bubbleView:[dict objectForKey:@"content"] from:YES withPosition:65]];
  114. }
  115. }else{
  116. photo = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
  117. [cell addSubview:photo];
  118. photo.image = [UIImage imageNamed:@"photo"];
  119. if ([[dict objectForKey:@"content"] isEqualToString:@"0"]) {
  120. [cell addSubview:[self yuyinView:1 from:NO withIndexRow:indexPath.row withPosition:65]];
  121. }else{
  122. [cell addSubview:[self bubbleView:[dict objectForKey:@"content"] from:NO withPosition:65]];
  123. }
  124. }
  125. return cell;
  126. }
  127. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. }
  130. @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高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)的更多相关文章

  1. iOS开发之微信聊天页面实现

    在上篇博客(iOS开发之微信聊天工具栏的封装)中对微信聊天页面下方的工具栏进行了封装,本篇博客中就使用之前封装的工具栏来进行聊天页面的编写.在聊天页面中主要用到了TableView的知识,还有如何在俩 ...

  2. iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码

    iOS精选源码 iOS高仿微信完整项目源码 Khala: Swift 编写的iOS/macOS 路由框架 微信左滑删除效果的实现与TableViewCell的常用样式介绍 实现阴影圆角并存,渐变色背景 ...

  3. iOS开发之微信聊天工具栏的封装

    之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...

  4. 实例源码--IOS高仿微信打飞机游戏(完整功能)

    下载源码 技术要点: 1. IOS游戏开发基础框架 2. 高仿打飞机游戏 3. 游戏背景音频技术 4.源码详细的中文注释 ……. 详细介绍: 1. IOS游戏开发基础框架 此套源码为涉及IOS游戏开发 ...

  5. iOS高仿微信悬浮窗、忍者小猪游戏、音乐播放器、支付宝、今日头条布局滚动效果等源码

    iOS精选源码 iOS WKWebView的使用源码 模仿apple music 小播放器的交互实现 高仿微信的悬浮小窗口 iOS仿支付宝首页效果 [swift]仿微信悬浮窗 类似于今日头条,网易新闻 ...

  6. IOS封装一个微信聊天的输入工具

    1.实现微信的输入工具 实现了大部分功能,各模块实现的很清晰,有利于更好的二次开发(适合自己的需求),我自己总结出来的, 可以更快的让你实现输入工具,不需要扩展的也可以很方便的使用这个输入工具. 1) ...

  7. iOS 即时通讯 + 仿微信聊天框架 + 源码

    这些你造吗? 即时通讯(IM),在IOS这片江湖里面已经算是一个老者了,我这小旋风也是在很早以前巡山的时候,就知道有即时通讯这个妖怪,以前也多多少少接触过一些,在造APP的时候用过,哎呀,说着说着就感 ...

  8. Android 高仿微信语音聊天页面高斯模糊效果

    目前的应用市场上,使用毛玻璃效果的APP随处可见,比如用过微信语音聊天的人可以发现,语音聊天页面就使用了高斯模糊效果. 先看下效果图: 仔细观察上图,我们可以发现,背景图以用户头像为模板,对其进行了高 ...

  9. iOS天气动画、高仿QQ菜单、放京东APP、高仿微信、推送消息等源码

    iOS精选源码 TYCyclePagerView iOS上的一个无限循环轮播图组件 iOS高仿微信完整项目源码 想要更简单的推送消息,看本文就对了 ScrollView嵌套ScrolloView解决方 ...

随机推荐

  1. 【Linux】find命令

    用途 find命令用于在指定目录下查找文件. 全称 无 参数 -name :后跟需要匹配的文件名模式,需要使用引号引起来 下面是一些简单的示例查找:(~表示$HOME目录) 1.查找当前$HOME下' ...

  2. DOM4J对于XML的用法

    一.基本使用方式     语法     1.获取根元素 Element root = document.getRootElement();     2.获取某个元素下的子元素 Element db_e ...

  3. 《数字图像处理原理与实践(MATLAB版)》一书之代码Part5

    <数字图像处理原理与实践(MATLAB版)>一书之代码Part5 本文系<数字图像处理原理与实践(MATLAB版)>一书之代码系列的Part5.辑录该书第225至第280页之代 ...

  4. According to TLD or attribute directive in tag file, attribute items does not accep t any expressions

    According to TLD or attribute directive in tag file, attribute items does not accep t any expression ...

  5. 【centos6】给php命令设置全局变量三种方式

    方法一:直接运行命令export PATH=$PATH:/usr/local/webserver/php/bin 和 export PATH=$PATH:/usr/local/webserver/my ...

  6. 【centos6】安装redis + phpredis 以及 常用配置参数

    1.redis-server和redis-cli安装文章:http://www.cnblogs.com/skyessay/p/6429988.html 1.前置条件:查看是否安装gcc,命令:gcc ...

  7. Xcode7 运行iOS10以上系统(10.1、10.2、10.3)解决Could not find Developer Disk Image

    由于历史原因,需要在Xcode7上真机运行下app,无奈手机系统已是10.3了,一运行, 就提示:Could not find Developer Disk Image 解决办法: 1.找到xcode ...

  8. Java8 新特性之流式数据处理(转)

    转自:https://www.cnblogs.com/shenlanzhizun/p/6027042.html 一. 流式处理简介 在我接触到java8流式处理的时候,我的第一感觉是流式处理让集合操作 ...

  9. <转>lua解析脚本过程中的关键数据结构介绍

    在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础.在整个过程中,比较重要的几个源码文件分别是:llex.h,lparse.h.l ...

  10. 更新卡片的zIndex

    问题描述 屏幕上有若干张互相重叠的卡片,用户每点击一张卡片,就要把这张卡片的移到最上面,也就是把它的zIndex置为最大值.应该如何操作每个卡片的zIndex才能实现? 实现方案一 定义一个全局变量g ...