ios开发聊天气泡实现
2016-08-08 15:18
513 查看
NSArray *arr = @[@"啊",@"啊飞",@"啊飞飞",@"啊飞飞起",@"啊飞飞起来啦",@"啊飞飞起来啦啦",@"啊飞飞起来啦啦啦",@"啊飞飞起来啦啦啦啦",@"啊飞飞起来啦啦啦啦啦啦"];
for (int i=0 ; i< arr.count; i++) {
NSString *bglabText = arr[i];
UIImageView *qipaoImg = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100 + 50 *i, [self receiveTextWidthWithString:bglabText] + 30, 35)];
UIImage *img = [UIImage imageNamed:@"box2"];
CGFloat scale = [UIScreen mainScreen].scale;
UIImage *dot9 = [img resizableImageWithCapInsets:(UIEdgeInsets){0/scale,20/scale,5/scale,20/scale} resizingMode:UIImageResizingModeStretch]; //{top/scale,left/scale,bottom/scale,right/scale} top left bottom right 表示图片不拉伸的尺寸.
qipaoImg.image = dot9;
[self.view addSubview:qipaoImg];
int xStart = 10;
UILabel *bglab = [[UILabel alloc] initWithFrame:CGRectMake(xStart, 5, qipaoImg.frame.size.width - xStart*2, qipaoImg.frame.size.height - 15)];
//bglab.numberOfLines = 0;
bglab.textAlignment = NSTextAlignmentCenter;
bglab.text = bglabText;
bglab.font = [UIFont systemFontOfSize:13];
[qipaoImg addSubview:bglab];
}
// 得到文字的宽度
-(int)receiveTextWidthWithString:(NSString *)str
{
CGSize maxWidth = CGSizeMake(MAXFLOAT, 20);
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
CGRect rect = [str boundingRectWithSize:maxWidth options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
int width = (int)rect.size.width;
return width;
}
气泡图片, 可以自行拖出来使用
补充, 特别短的图片适合文字少的, 长点的图片适合文字多的,具体适配得具体调节.这里只是给出了方法, 具体使用还得调 啊.
相关文章推荐
- ios开发聊天气泡实现
- iOS开发之微信聊天页面实现
- IOS开发学习笔记043-QQ聊天界面实现
- 【iOS开发】即时视频聊天实现
- IOS开发,XMPP实现聊天,即时通讯(一)
- iOS 图片 的 聊天气泡显示 Objective-C 和 Swift,如何实现聊天软件的图片显示的剪裁后的气泡呢!!!
- iOS开发之聊天模块--内容保存逻辑实现
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- IOS开发,XMPP实现聊天,即时通讯(二)
- iOS开发之QQ中未读气泡拖拽消失的实现分析
- iOS 类似微信,QQ聊天界面的气泡聊天简单实现Demo
- iOS 使用XMPP框架开发IM聊天模块,实现简单的文字聊天
- IOS开发之使用Speex格式实现简单的语音聊天功能(一)
- iOS开发:聊天输入框的实现
- iOS开发利用系统推送Notifaction和轮询实现简单聊天系统
- IOS开发之使用Speex格式实现简单的语音聊天功能(一)
- IOS开发之使用Speex格式实现简单的语音聊天功能(二)
- IOS开发,XMPP实现聊天,即时通讯(三)
- iOS开发:QQ中未读气泡拖拽消失的实现分析(KittenYang)
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!