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解决方 ...
随机推荐
- 【BI】商务智能
BI的定义 商务智能BI (Business Intellignece) 商务智能是涵盖性术语,包含框架.工具.数据库.分析工具.应用和方法.商务智能的主要目标是实现数据的交互,实现对数据的操作,供管 ...
- java 怎么实现接收控制台输入?
//接收键盘输入: //怎么实现接收控制台输入? import java.util.Scanner; //Test.java import java.util.Scanner; public clas ...
- grep命令-v参数过滤以井号、分号开头的注释信息行及空白行
grep命令-v参数(反向选择)分别去掉所有以#(井号)和;(分号)开头的注释信息行,对于剩余的空白行可以再用^$来表示并反选过滤 [root@rhel7 samba]# cat smb.conf | ...
- CentOS下nodejs最简单的安装方法
1. 下载编译好的文件 我的系统是centos7,进入要存放下载资源的目录,个人建议存放在/usr/local/src/目录下.然后执行安装命令: wget http://nodejs.org/dis ...
- Swift3 Scanner用法之判断是否数字、提取字符串里的数字
1.判断是否数字 /// 判断是否是数字 /// /// - Parameter string: <#string description#> /// - Returns: <#re ...
- iOS 10 的一个重要更新-用 UIViewPropertyAnimator 编写动画
曾经的黑暗年代 用基于 block 的 UIView animation 来编写 view 属性(frame, transform 等等)变化的动画非常简单.只需要短短几行代码: view.alpha ...
- Eclipse插件的安装与配置
1.下载插件时注意要和Eclipse版本兼容. 2.安装Eclipse插件时注意是否要安装其他的插件,这一点很容易被忽视. 3.有时启动Eclipse未加载插件,解决方法很多,总结一下: a ...
- MS SQL Server查询优化方法 查询速度慢的原因很多,常见如下几种
1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 5.网络速度慢 6.查询出的数据量过大 ...
- Linux批量kill进程
使用awk批量杀进程的命令: ps -ef | grep firefox | grep -v grep | awk '{print "kill -9 "$2}'|sh 说明: #列 ...
- 启用跨源请求 (CORS)
https://docs.microsoft.com/zh-cn/aspnet/core/security/cors