diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 871fae6f4..51be144cf 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -1256,9 +1256,6 @@ static void linphone_iphone_popup_password_request(LinphoneCore *lc, LinphoneAut const char *filename = linphone_content_get_name(file); [msgData setObject:[NSString stringWithUTF8String:filename] forKey:@"msg"]; } else { - CGSize msgSize = [UIChatBubbleTextCell ViewSizeForMessage:msg withWidth:375]; - [msgData setObject:[NSNumber numberWithFloat:msgSize.width] forKey:@"width"]; - [msgData setObject:[NSNumber numberWithFloat:msgSize.height] forKey:@"height"]; [msgData setObject:[UIChatBubbleTextCell TextMessageForChat:msg] forKey:@"msg"]; } [msgData setObject:[NSNumber numberWithBool:isOutgoing] forKey:@"isOutgoing"]; diff --git a/richNotifications/NotificationTableViewCell.h b/richNotifications/NotificationTableViewCell.h index 58fb529cb..2300cf3b4 100644 --- a/richNotifications/NotificationTableViewCell.h +++ b/richNotifications/NotificationTableViewCell.h @@ -10,7 +10,7 @@ @interface NotificationTableViewCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *contactImage; @property (weak, nonatomic) IBOutlet UILabel *nameDate; -@property (weak, nonatomic) IBOutlet UITextView *msgText; +@property (strong, nonatomic) IBOutlet UITextView *msgText; @property (weak, nonatomic) IBOutlet UILabel *imdm; @property (weak, nonatomic) IBOutlet UIImageView *background; @property (weak, nonatomic) IBOutlet UIImageView *bottomBarColor; @@ -19,4 +19,7 @@ @property float width; @property float height; +- (CGSize)ViewSizeForMessage:(NSString *)chat withWidth:(int)width; +- (CGSize)ViewHeightForMessage:(NSString *)messageText withWidth:(int)width; + @end diff --git a/richNotifications/NotificationTableViewCell.m b/richNotifications/NotificationTableViewCell.m index 79b64f1b1..b1b2160e3 100644 --- a/richNotifications/NotificationTableViewCell.m +++ b/richNotifications/NotificationTableViewCell.m @@ -39,4 +39,56 @@ _msgText.textContainer.lineFragmentPadding = 0; } +#pragma mark - Bubble size computing + +- (CGSize)computeBoundingBox:(NSString *)text size:(CGSize)size font:(UIFont *)font { + if (!text || text.length == 0) + return CGSizeMake(0, 0); + + return [text boundingRectWithSize:size + options:(NSStringDrawingUsesLineFragmentOrigin | + NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading) + attributes:@{ + NSFontAttributeName : font + } + context:nil].size; +} + +static const CGFloat CELL_MIN_HEIGHT = 60.0f; +static const CGFloat CELL_MIN_WIDTH = 190.0f; +static const CGFloat CELL_MESSAGE_X_MARGIN = 78 + 10.0f; +static const CGFloat CELL_MESSAGE_Y_MARGIN = 52; // 44; + +- (CGSize)ViewHeightForMessage:(NSString *)messageText withWidth:(int)width { + static UIFont *messageFont = nil; + + if (!messageFont) { + messageFont = _msgText.font; + } + CGSize size; + size = [self computeBoundingBox:messageText + size:CGSizeMake(width - CELL_MESSAGE_X_MARGIN - 4, CGFLOAT_MAX) + font:messageFont]; + + size.width = MAX(size.width + CELL_MESSAGE_X_MARGIN, CELL_MIN_WIDTH); + size.height = MAX(size.height + CELL_MESSAGE_Y_MARGIN, CELL_MIN_HEIGHT); + return size; +} +- (CGSize)ViewSizeForMessage:(NSString *)chat withWidth:(int)width { + static UIFont *dateFont = nil; + static CGSize dateViewSize; + + if (!dateFont) { + dateFont = _nameDate.font; + dateViewSize = _nameDate.frame.size; + dateViewSize.width = CGFLOAT_MAX; + } + + CGSize messageSize = [self ViewHeightForMessage:chat withWidth:width]; + CGSize dateSize = [self computeBoundingBox:_nameDate.text size:dateViewSize font:dateFont]; + messageSize.width = MAX(MAX(messageSize.width, MIN(dateSize.width + CELL_MESSAGE_X_MARGIN, width)), CELL_MIN_WIDTH); + + return messageSize; +} + @end diff --git a/richNotifications/NotificationViewController.m b/richNotifications/NotificationViewController.m index 19d4e0075..49f0175ce 100644 --- a/richNotifications/NotificationViewController.m +++ b/richNotifications/NotificationViewController.m @@ -38,7 +38,6 @@ NSLog(@"Content length : %f", self.tableView.contentSize.height); NSLog(@"Number of rows : %d", (unsigned int)[self tableView:self.tableView numberOfRowsInSection:0]); [self.view.superview bringSubviewToFront:self.tableView]; - self.tableView.bounds = CGRectMake(self.tableView.bounds.origin.x, self.tableView.bounds.origin.y, self.tableView.contentSize.width, self.tableView.contentSize.height); NSLog(@"View length : %f", self.tableView.bounds.size.height); } @@ -68,8 +67,9 @@ cell.nameDate.text = display; cell.msgText.text = msgText; cell.isOutgoing = isOutgoing; - cell.width = ((NSNumber *)[msgs[indexPath.row] objectForKey:@"width"]).floatValue; - cell.height = ((NSNumber *)[msgs[indexPath.row] objectForKey:@"height"]).floatValue; + CGSize size = [cell ViewSizeForMessage:msgText withWidth:self.view.bounds.size.width - 10]; + cell.width = size.width; + cell.height = size.height; cell.nameDate.textColor = [UIColor colorWithPatternImage:cell.background.image]; cell.msgText.textColor = [UIColor darkGrayColor]; if (!isOutgoing) { @@ -94,7 +94,11 @@ #pragma mark - UITableViewDelegate Functions - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - return ((NSNumber *)[msgs[indexPath.row] objectForKey:@"height"]).floatValue + 5; + NotificationTableViewCell *cell = [[NotificationTableViewCell alloc] init]; + cell.msgText = [[UITextView alloc] init]; + cell.msgText.text = ((NSString *)[msgs[indexPath.row] objectForKey:@"msg"]); + cell.msgText.font = [UIFont systemFontOfSize:17]; + return [cell ViewHeightForMessage:cell.msgText.text withWidth:self.view.bounds.size.width - 10].height + 5; } @end