Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-13-2010, 06:59 AM   #1 (permalink)
Registered Member
 
tobyaherbert's Avatar
 
Join Date: Nov 2010
Posts: 19
tobyaherbert is on a distinguished road
Post iPhone SMS Chat Bubble Alignment

I am creating my first app and it is a bluetooth chat app. I decided to use sms style chat bubbles but am having some problems with the alignment. If you have any sent messages and then receive a message, all the messages change sides. This also happens the other way around. Any ideas? I am using a UITableView for this. Here is part of my .h file:

Code:
NSMutableArray *conversationArray;
	IBOutlet UITableView *tbl;
	IBOutlet UITextField *field;
	IBOutlet UIToolbar *toolbar;
	NSMutableArray *messages;
	UIImageView *balloonView;
	UILabel *label;
	UIImage *balloon;
	BOOL wasRecieved;
	BOOL wasSent;
}

@property (nonatomic, retain) GKSession *currentSession;
@property (nonatomic, retain) UITableView *tbl;
@property (nonatomic, retain) UITextField *field;
@property (nonatomic, retain) UIToolbar *toolbar;
@property (nonatomic, retain) NSMutableArray *messages;

- (IBAction)sendMessage:(id)sender;
And this is part of my .m file:
Code:
#pragma mark -
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [messages count];
}

- (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] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		tableView.separatorStyle = UITableViewCellSeparatorStyleNone;		
		
		balloonView = [[UIImageView alloc] initWithFrame:CGRectZero];
		balloonView.tag = 1;
		
		label = [[UILabel alloc] init];
		label.backgroundColor = [UIColor clearColor];
		label.tag = 2;
		label.numberOfLines = 0;
		label.lineBreakMode = UILineBreakModeWordWrap;
		label.font = [UIFont systemFontOfSize:14.0];
		
		UIView *message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
		message.tag = 0;
		[message addSubview:balloonView];
		[message addSubview:label];
		
		message.autoresizingMask = UIViewAutoresizingFlexibleWidth;
		
		[cell.contentView addSubview:message];
		
		[balloonView release];
		[label release];
		[message release];
	}
	else
	{
		balloonView = (UIImageView *)[[cell.contentView viewWithTag:0] viewWithTag:1];
		label = (UILabel *)[[cell.contentView viewWithTag:0] viewWithTag:2];
	}
	
	NSString *text = [messages objectAtIndex:indexPath.row];
	CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
	
	if (wasRecieved == YES) {
		balloonView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
		balloonView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
		balloon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
		label.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height);
		label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
	} else if (wasRecieved == NO) {
		balloonView.frame = CGRectMake(0.0, 2.0, size.width + 28, size.height + 15);
		balloonView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
		balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
		label.frame = CGRectMake(16, 8, size.width + 5, size.height);
		label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
	}
	
	balloonView.image = balloon;
	label.text = text;
	
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSString *body = [messages objectAtIndex:indexPath.row];
	CGSize size = [body sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
	return size.height + 15;
}

// Receive & send Controllers

- (void) mySendDataToPeers:(NSData *)data {
    if (currentSession) {
		NSLog(@"Data Sent!");
        [self.currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
		if(![field.text isEqualToString:@""])
		{
			wasSent = NO;
			[messages addObject:field.text];
			[tbl reloadData];
			
			field.text = @"";
			
			[self textFieldShouldReturn:nil];
			[field resignFirstResponder];
		}
	}
}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {
	NSLog(@"Data Recieved!");
	NSString *str;
    str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	if (vibration.on) {
		AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
	}
	if (alertTone.on) {
		[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(audioService) userInfo:nil repeats:NO];
	}
	wasRecieved = YES;
		[messages addObject:str];
		[tbl reloadData];
	[str release];
	str = nil;
}
Thanks in advance!
tobyaherbert is offline   Reply With Quote
Old 01-21-2011, 05:26 AM   #2 (permalink)
JaGDiSH
 
Join Date: Sep 2010
Location: Ahmedabad
Posts: 57
jagds is on a distinguished road
Send a message via Skype™ to jagds
Default

Hey..
R u using UITableView as dynamic or not..?
Please tell me i am also trying to do that..
But it get crashed on this code

Code here :
if (wasReceived == YES)
{
chatimageView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
chatimageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
ballon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
chatLabel.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height);
chatLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
}
else if (wasReceived == NO)
{
chatimageView.frame = CGRectMake(0.0f, 2.0f, size.width + 28.0f, size.height + 15.0f);
chatimageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
ballon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
chatLabel.frame = CGRectMake(16.0f, 8.0f, size.width + 5.0f, size.height);
chatLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}
chatimageView.image = ballon;
chatLabel.text = text;


Please tell me and suggest me...
jagds is offline   Reply With Quote
Old 02-20-2011, 04:59 AM   #3 (permalink)
Registered Member
 
tobyaherbert's Avatar
 
Join Date: Nov 2010
Posts: 19
tobyaherbert is on a distinguished road
Default Chat Alignment

Quote:
Originally Posted by jagds View Post
Hey..
R u using UITableView as dynamic or not..?
Please tell me i am also trying to do that..
But it get crashed on this code

Code here :
if (wasReceived == YES)
{
chatimageView.frame = CGRectMake(320.0f - (size.width + 28.0f), 2.0f, size.width + 28.0f, size.height + 15.0f);
chatimageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
ballon = [[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
chatLabel.frame = CGRectMake(307.0f - (size.width + 5.0f), 8.0f, size.width + 5.0f, size.height);
chatLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
}
else if (wasReceived == NO)
{
chatimageView.frame = CGRectMake(0.0f, 2.0f, size.width + 28.0f, size.height + 15.0f);
chatimageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
ballon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
chatLabel.frame = CGRectMake(16.0f, 8.0f, size.width + 5.0f, size.height);
chatLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}
chatimageView.image = ballon;
chatLabel.text = text;


Please tell me and suggest me...
I dont Remember i think i used non-dynamic tableview not sure though.
Regards
Toby...
tobyaherbert is offline   Reply With Quote
Reply

Bookmarks

Tags
balloon, bluetooth, bubbles, chat, sms

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 372
7 members and 365 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:42 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0