大家好,百忙之中,抽出點空,寫個微博,話說好久沒寫。
最近項目中有碰到寫類似微信聊天界面上的效果,特整理了一下,寫了一個小的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!