almost 11 years ago
Following code will help you to create dynamic UITableViewCell w.r.t to its content of message.
- -(void)setChatData:(ChatMessage *)messageObj memberDict:(NSDictionary *)memberDict isCurrentUser:(BOOL)isCurrentUser{
- NSDictionary *userDict = [memberDict objectForKey:messageObj.senderId];
- [message setText:messageObj.body];
- CGRect BGframeintial = backgoundView.frame;
- BGframeintial.size.width = MIN_WIDTH + 10;
- backgoundView.frame = BGframeintial;
- CGRect messgframeintial = message.frame;
- messgframeintial.size.width = MIN_WIDTH;
- message.frame = messgframeintial;
- // message.numberOfLines = 1;
- [message sizeToFit];
- [self checkForMaxLimit];
- [message sizeToFit];
- [self setFrameForCell:isCurrentUser];
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- // [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
- // NSDate *date = [formatter dateFromString:[dict valueForKey:@"time"]];
- NSDate *date = messageObj.timeObject;
- if (date == nil) {
- [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
- date = [formatter dateFromString:messageObj.timeStamp];
- }
- [formatter setDateFormat:@"MMM,dd hh:mm a"];
- NSString *chatTime = [formatter stringFromDate:date];
- [time setText:chatTime];
- [time setFont:[UIFont systemFontOfSize:8]];
- [userName setText:[userDict objectForKey:@"name"]];
- [userImage setImageFromUrl:[userDict objectForKey:@"url"] borderColor:[UIColor whiteColor]];
- [self setBackgroundColor:[UIColor clearColor]];
- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
- }
- -(void)setFrameForCell:(BOOL)iscurrentUser{
- CGRect frame = backgoundView.frame;
- frame.size.height = message.frame.size.height + 18;
- frame.size.width = message.frame.size.width + 10;
- if (iscurrentUser) {
- frame.origin.x = (CURENT_USER_LIMITED - frame.size.width);
- CGRect messgframeintial = message.frame;
- messgframeintial.origin.x = frame.origin.x + 5;
- message.frame = messgframeintial;
- }
- backgoundView.frame = frame;
- frame = time.frame;
- frame.origin.y = backgoundView.frame.origin.y + backgoundView.frame.size.height+10 - 20;
- time.frame = frame;
- backgoundView.layer.cornerRadius = 5;
- }
- -(void)checkForMaxLimit{
- CGSize labelSize = [message.text sizeWithFont:message.font
- constrainedToSize:message.frame.size
- lineBreakMode:NSLineBreakByWordWrapping];
- int numberOfLine = ceilf(labelSize.height);
- if (numberOfLine/17 > 4) {
- CGRect BGframeintial = backgoundView.frame;
- BGframeintial.size.width = MAX_WIDTH + 10;
- backgoundView.frame = BGframeintial;
- CGRect messgframeintial = message.frame;
- messgframeintial.size.width = MAX_WIDTH;
- message.frame = messgframeintial;
- }
- }
-(void)setChatData:(ChatMessage *)messageObj memberDict:(NSDictionary *)memberDict isCurrentUser:(BOOL)isCurrentUser{ NSDictionary *userDict = [memberDict objectForKey:messageObj.senderId]; [message setText:messageObj.body]; CGRect BGframeintial = backgoundView.frame; BGframeintial.size.width = MIN_WIDTH + 10; backgoundView.frame = BGframeintial; CGRect messgframeintial = message.frame; messgframeintial.size.width = MIN_WIDTH; message.frame = messgframeintial; // message.numberOfLines = 1; [message sizeToFit]; [self checkForMaxLimit]; [message sizeToFit]; [self setFrameForCell:isCurrentUser]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; // NSDate *date = [formatter dateFromString:[dict valueForKey:@"time"]]; NSDate *date = messageObj.timeObject; if (date == nil) { [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; date = [formatter dateFromString:messageObj.timeStamp]; } [formatter setDateFormat:@"MMM,dd hh:mm a"]; NSString *chatTime = [formatter stringFromDate:date]; [time setText:chatTime]; [time setFont:[UIFont systemFontOfSize:8]]; [userName setText:[userDict objectForKey:@"name"]]; [userImage setImageFromUrl:[userDict objectForKey:@"url"] borderColor:[UIColor whiteColor]]; [self setBackgroundColor:[UIColor clearColor]]; [self setSelectionStyle:UITableViewCellSelectionStyleNone]; } -(void)setFrameForCell:(BOOL)iscurrentUser{ CGRect frame = backgoundView.frame; frame.size.height = message.frame.size.height + 18; frame.size.width = message.frame.size.width + 10; if (iscurrentUser) { frame.origin.x = (CURENT_USER_LIMITED - frame.size.width); CGRect messgframeintial = message.frame; messgframeintial.origin.x = frame.origin.x + 5; message.frame = messgframeintial; } backgoundView.frame = frame; frame = time.frame; frame.origin.y = backgoundView.frame.origin.y + backgoundView.frame.size.height+10 - 20; time.frame = frame; backgoundView.layer.cornerRadius = 5; } -(void)checkForMaxLimit{ CGSize labelSize = [message.text sizeWithFont:message.font constrainedToSize:message.frame.size lineBreakMode:NSLineBreakByWordWrapping]; int numberOfLine = ceilf(labelSize.height); if (numberOfLine/17 > 4) { CGRect BGframeintial = backgoundView.frame; BGframeintial.size.width = MAX_WIDTH + 10; backgoundView.frame = BGframeintial; CGRect messgframeintial = message.frame; messgframeintial.size.width = MAX_WIDTH; message.frame = messgframeintial; } }
0 Comment(s)