IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
大家好,百忙之中,抽出点空,写个微博,话说好久没写。
最近项目中有碰到写类似微信聊天界面上的效果,特整理了一下,写了一个小的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高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)的更多相关文章
- iOS开发之微信聊天页面实现
		
在上篇博客(iOS开发之微信聊天工具栏的封装)中对微信聊天页面下方的工具栏进行了封装,本篇博客中就使用之前封装的工具栏来进行聊天页面的编写.在聊天页面中主要用到了TableView的知识,还有如何在俩 ...
 - iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码
		
iOS精选源码 iOS高仿微信完整项目源码 Khala: Swift 编写的iOS/macOS 路由框架 微信左滑删除效果的实现与TableViewCell的常用样式介绍 实现阴影圆角并存,渐变色背景 ...
 - iOS开发之微信聊天工具栏的封装
		
之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...
 - 实例源码--IOS高仿微信打飞机游戏(完整功能)
		
下载源码 技术要点: 1. IOS游戏开发基础框架 2. 高仿打飞机游戏 3. 游戏背景音频技术 4.源码详细的中文注释 ……. 详细介绍: 1. IOS游戏开发基础框架 此套源码为涉及IOS游戏开发 ...
 - iOS高仿微信悬浮窗、忍者小猪游戏、音乐播放器、支付宝、今日头条布局滚动效果等源码
		
iOS精选源码 iOS WKWebView的使用源码 模仿apple music 小播放器的交互实现 高仿微信的悬浮小窗口 iOS仿支付宝首页效果 [swift]仿微信悬浮窗 类似于今日头条,网易新闻 ...
 - IOS封装一个微信聊天的输入工具
		
1.实现微信的输入工具 实现了大部分功能,各模块实现的很清晰,有利于更好的二次开发(适合自己的需求),我自己总结出来的, 可以更快的让你实现输入工具,不需要扩展的也可以很方便的使用这个输入工具. 1) ...
 - iOS  即时通讯 + 仿微信聊天框架 + 源码
		
这些你造吗? 即时通讯(IM),在IOS这片江湖里面已经算是一个老者了,我这小旋风也是在很早以前巡山的时候,就知道有即时通讯这个妖怪,以前也多多少少接触过一些,在造APP的时候用过,哎呀,说着说着就感 ...
 - Android 高仿微信语音聊天页面高斯模糊效果
		
目前的应用市场上,使用毛玻璃效果的APP随处可见,比如用过微信语音聊天的人可以发现,语音聊天页面就使用了高斯模糊效果. 先看下效果图: 仔细观察上图,我们可以发现,背景图以用户头像为模板,对其进行了高 ...
 - iOS天气动画、高仿QQ菜单、放京东APP、高仿微信、推送消息等源码
		
iOS精选源码 TYCyclePagerView iOS上的一个无限循环轮播图组件 iOS高仿微信完整项目源码 想要更简单的推送消息,看本文就对了 ScrollView嵌套ScrolloView解决方 ...
 
随机推荐
- 【Linux】find命令
			
用途 find命令用于在指定目录下查找文件. 全称 无 参数 -name :后跟需要匹配的文件名模式,需要使用引号引起来 下面是一些简单的示例查找:(~表示$HOME目录) 1.查找当前$HOME下' ...
 - DOM4J对于XML的用法
			
一.基本使用方式 语法 1.获取根元素 Element root = document.getRootElement(); 2.获取某个元素下的子元素 Element db_e ...
 - 《数字图像处理原理与实践(MATLAB版)》一书之代码Part5
			
<数字图像处理原理与实践(MATLAB版)>一书之代码Part5 本文系<数字图像处理原理与实践(MATLAB版)>一书之代码系列的Part5.辑录该书第225至第280页之代 ...
 - 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 ...
 - 【centos6】给php命令设置全局变量三种方式
			
方法一:直接运行命令export PATH=$PATH:/usr/local/webserver/php/bin 和 export PATH=$PATH:/usr/local/webserver/my ...
 - 【centos6】安装redis + phpredis 以及 常用配置参数
			
1.redis-server和redis-cli安装文章:http://www.cnblogs.com/skyessay/p/6429988.html 1.前置条件:查看是否安装gcc,命令:gcc ...
 - 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 ...
 - Java8 新特性之流式数据处理(转)
			
转自:https://www.cnblogs.com/shenlanzhizun/p/6027042.html 一. 流式处理简介 在我接触到java8流式处理的时候,我的第一感觉是流式处理让集合操作 ...
 - <转>lua解析脚本过程中的关键数据结构介绍
			
在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础.在整个过程中,比较重要的几个源码文件分别是:llex.h,lparse.h.l ...
 - 更新卡片的zIndex
			
问题描述 屏幕上有若干张互相重叠的卡片,用户每点击一张卡片,就要把这张卡片的移到最上面,也就是把它的zIndex置为最大值.应该如何操作每个卡片的zIndex才能实现? 实现方案一 定义一个全局变量g ...