My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Thursday 25 July 2013

SMS-LIKE CHAT BUBBLES ON IPHONE

ShowComment.h
==========
#import <UIKit/UIKit.h>
@interface ShowComment : UITableViewController<UITextFieldDelegate,UITextViewDelegate>{

}
@property (nonatomic,retain) NSMutableArray *commentList;
@property (nonatomic,retain) NSMutableArray *userList;
@end


ShowComment.m
==============

//  Created by Rajendra on 22/07/13.
//
//

#import "ShowComment.h"

#import <QuartzCore/QuartzCore.h>
@interface ShowComment ()

@end

@implementation ShowComment
@synthesize commentList,userList;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad]; 
    self.commentList=[[NSMutableArray alloc] initWithObjects:@"Hello",@"Hi",@"How are you?",@"Great..& u ?",@"Fine.Why didn't you come lastday? ",@"I was sick", nil];
    self.userList=[[NSMutableArray alloc] initWithObjects:@"Raju",@"Rabi",@"Raju",@"Rabi",@"Raju",@"Rabi", nil];
    if ([self.tableView respondsToSelector:@selector(backgroundView)])
        self.tableView.backgroundView = nil;
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"inter_bg.jpg"]];
    [self.tableView setBackgroundView:bg];
   
    self.navigationItem.title=@"Bubble Demo";
   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
  
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  
     int count=[self.commentList count];
   
            return count;
 
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSInteger unameTag = 1;
    static NSInteger msgTag = 2;   
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   
  
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier] ;
       
        cell.accessoryType = UITableViewCellAccessoryNone;
        UITextView *userName = [[UITextView alloc] init];
        userName.backgroundColor = [UIColor clearColor];
        userName.editable = NO;
        userName.scrollEnabled = NO;
        [userName sizeToFit];
        userName.tag=unameTag;
        [cell.contentView addSubview:userName];
       
        UITextView *msgText = [[UITextView alloc] init];
        msgText.backgroundColor = [UIColor clearColor];
        msgText.editable = NO;
        msgText.scrollEnabled = NO;
        [msgText sizeToFit];
        msgText.tag=msgTag;
        [cell.contentView addSubview:msgText];   
    }
    UITextView *uTv = (UITextView *)[cell.contentView viewWithTag:unameTag];
    UITextView *msgTv = (UITextView *)[cell.contentView viewWithTag:msgTag];   
  
    CGFloat result = 20.0;
    CGFloat result1 = 20.0;
    CGSize textSize = { 230.0, 20000.0 };
   
    NSString *oneUsername=[self.userList objectAtIndex:indexPath.section];
    NSString *oneComment=[self.commentList objectAtIndex:indexPath.section];   
    CGSize size = [ oneComment sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
    CGSize size1 = [ oneUsername sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];   
    result = MAX(size.height - 5.0, 30.0);
    result1 = MAX(size1.height - 5.0, 30.0);
   
    uTv.text=oneUsername;
    msgTv.text=oneComment;
   
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];   
    [cell setFrame:CGRectMake(0, 0, size.width, size.height)];   
    if ( indexPath.section % 2 == 0 ) {
        [uTv setFrame:CGRectMake(5.0, 35.0, 50.0, result1)];
        [msgTv setFrame:CGRectMake(70.0, 10.0, size.width+30, result)];
       
        UIImage* balloon = [[UIImage imageNamed:@"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
        UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, size.width+55, result+20)];
        UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
       
        [newImage setImage:balloon];
        [newView addSubview:newImage];
        [cell setBackgroundView:newView];   
    }
    else
    {
        [uTv setFrame:CGRectMake(265.0, 35.0, 50.0, result1)];
        [msgTv setFrame:CGRectMake(220.0-size.width, 10.0, size.width+30, result)];
        UIImage* balloon = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
        UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(210.0-size.width, 0.0, size.width+55, result+20)];
        UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0, 0.0, cell.frame.size.width, cell.frame.size.height)];
       
        [newImage setImage:balloon];
        [newView addSubview:newImage];
        [cell setBackgroundView:newView];
    }
  
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {   
    if ([self.commentList count] != 0) {       
        CGSize textSize = { 260.0, 20000.0 };       
        NSString *oneComment=[self.commentList objectAtIndex:indexPath.section];       
        CGSize size = [oneComment sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
        size.height += 5;       
        CGFloat height = size.height<36?36:size.height;       
        return height;
    }
    return 80;   
}

@end







bubbleMine.png


bubbleOther.png  
Download Project

2 comments:

  1. Awesome Blog. Thanks. Do keep posting such good blogs. Thanks for sharing Informative posts.

    -------------------------------------------------------------------------------
    iPhone App Development :: && ::Mobile and Web Application Development

    ReplyDelete