diff --git a/README.md b/README.md index 1eed2c5..2d3e382 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Easy integrated chat feature. pod 'ZHChat', '~> 0.1.4' -### 复制文件夹ZHCMessagesViewController至你的工程中 +### Copy the folder ZHCMessagesViewController to your project ## Getting Started *Getting started guide for ZHChat* diff --git a/ZHCMessagesViewController/Controllers/ZHCMessagesViewController.m b/ZHCMessagesViewController/Controllers/ZHCMessagesViewController.m index bc91d3c..c7857c6 100644 --- a/ZHCMessagesViewController/Controllers/ZHCMessagesViewController.m +++ b/ZHCMessagesViewController/Controllers/ZHCMessagesViewController.m @@ -22,78 +22,9 @@ #import "UIColor+ZHCMessages.h" #import "UIView+ZHCMessages.h" - - #import #import - -static IMP ZHCReplaceMethodWithBlock(Class c, SEL origSEL, id block) { - NSCParameterAssert(block); - - // get original method - Method origMethod = class_getInstanceMethod(c, origSEL); - NSCParameterAssert(origMethod); - - // convert block to IMP trampoline and replace method implementation - IMP newIMP = imp_implementationWithBlock(block); - - // Try adding the method if not yet in the current class - if (!class_addMethod(c, origSEL, newIMP, method_getTypeEncoding(origMethod))) { - return method_setImplementation(origMethod, newIMP); - } else { - return method_getImplementation(origMethod); - } -} - -static void ZHCInstallWorkaroundForSheetPresentationIssue26295020(void) { - __block void (^removeWorkaround)(void) = ^{}; - const void (^installWorkaround)(void) = ^{ - const SEL presentSEL = @selector(presentViewController:animated:completion:); - __block IMP origIMP = ZHCReplaceMethodWithBlock(UIViewController.class, presentSEL, ^(UIViewController *self, id vC, BOOL animated, id completion) { - UIViewController *targetVC = self; - while (targetVC.presentedViewController) { - targetVC = targetVC.presentedViewController; - } - ((void (*)(id, SEL, id, BOOL, id))origIMP)(targetVC, presentSEL, vC, animated, completion); - }); - removeWorkaround = ^{ - Method origMethod = class_getInstanceMethod(UIViewController.class, presentSEL); - NSCParameterAssert(origMethod); - class_replaceMethod(UIViewController.class, - presentSEL, - origIMP, - method_getTypeEncoding(origMethod)); - }; - }; - - const SEL presentSheetSEL = NSSelectorFromString(@"presentSheetFromRect:"); - const void (^swizzleOnClass)(Class k) = ^(Class klass) { - const __block IMP origIMP = ZHCReplaceMethodWithBlock(klass, presentSheetSEL, ^(id self, CGRect rect) { - // Before calling the original implementation, we swizzle the presentation logic on UIViewController - installWorkaround(); - // UIKit later presents the sheet on [view.window rootViewController]; - // See https://github.com/WebKit/webkit/blob/1aceb9ed7a42d0a5ed11558c72bcd57068b642e7/Source/WebKit2/UIProcess/ios/WKActionSheet.mm#L102 - // Our workaround forwards this to the topmost presentedViewController instead. - ((void (*)(id, SEL, CGRect))origIMP)(self, presentSheetSEL, rect); - // Cleaning up again - this workaround would swallow bugs if we let it be there. - removeWorkaround(); - }); - }; - - // _UIRotatingAlertController - Class alertClass = NSClassFromString([NSString stringWithFormat:@"%@%@%@", @"_U", @"IRotat", @"ingAlertController"]); - if (alertClass) { - swizzleOnClass(alertClass); - } - - // WKActionSheet - Class actionSheetClass = NSClassFromString([NSString stringWithFormat:@"%@%@%@", @"W", @"KActio", @"nSheet"]); - if (actionSheetClass) { - swizzleOnClass(actionSheetClass); - } -} - @interface ZHCMessagesViewController () @property (weak, nonatomic) IBOutlet ZHCMessagesTableView *messageTableView; @property (strong, nonatomic) IBOutlet ZHCMessagesInputToolbar *inputMessageBarView; @@ -122,16 +53,8 @@ + (instancetype)messagesViewController bundle:[NSBundle bundleForClass:[ZHCMessagesViewController class]]]; } -+ (void)initialize { - [super initialize]; - if (self == [ZHCMessagesViewController self]) { - ZHCInstallWorkaroundForSheetPresentationIssue26295020(); - } -} - #pragma mark - Initialization - - (void)zhc_configureMessagesViewController { @@ -401,9 +324,7 @@ - (void)scrollToBottomAnimated:(BOOL)animated return; } NSInteger rows = [self.messageTableView numberOfRowsInSection:0]; - NSLog(@"rows:%ld",(long)rows); NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:(rows - 1) inSection:0]; - [self scrollToIndexPath:lastCellIndexPath animated:animated]; } @@ -425,7 +346,6 @@ - (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated if (isContentTooSmall) { // workaround for the first few messages not scrolling // when the TableView view content size is too small, `scrollToItemAtIndexPath:` doesn't work properly - // this seems to be a UIKit bug, see #256 on GitHub [self.messageTableView scrollRectToVisible:CGRectMake(0.0, tableViewContentHeight - 1.0f, 1.0f, 1.0f) animated:animated]; return; @@ -434,7 +354,6 @@ - (void)scrollToIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated indexPath = [NSIndexPath indexPathForRow:row inSection:0]; // workaround for really long messages not scrolling // if last message is too long, use scroll position bottom for better appearance, else use top - // possibly a UIKit bug, see #480 on GitHub CGFloat cellHeight = [self tableView:self.messageTableView heightForRowAtIndexPath:indexPath]; CGFloat maxHeightForVisibleMessage = CGRectGetHeight(self.messageTableView.bounds) - self.messageTableView.contentInset.top @@ -490,19 +409,15 @@ -(CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForRowAtIndexPath:(N }else{ avatarHeight = tableView.tableViewLayout.incomingAvatarViewSize.height; } - if (size.height<=avatarHeight) { - NSLog(@"1111"); - }else{ - NSLog(@"0000"); - } + CGFloat bubbleHeight = size.height>avatarHeight?size.height:avatarHeight; - CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath]; + CGFloat cellsSpaceLabelHeight = [tableView.dataSource tableView:tableView tableViewCellSeparatorHeightAtIndexpath:indexPath]; + CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForCellTopLabelAtIndexPath:indexPath]; CGFloat cellBubbleTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath]; CGFloat cellBottomLabelHeight = [tableView.dataSource tableView:tableView heightForCellBottomLabelAtIndexPath:indexPath]; - height = kZHCMessagesTableViewCellSpaceDefault + cellTopLabelHeight + cellBubbleTopLabelHeight + cellBottomLabelHeight + bubbleHeight + 2.0*[UIScreen mainScreen].scale; - NSLog(@"cellHeight:%f",height); + height = cellsSpaceLabelHeight + cellTopLabelHeight + cellBubbleTopLabelHeight + cellBottomLabelHeight + bubbleHeight + 2.0*[UIScreen mainScreen].scale; return height; } @@ -567,11 +482,12 @@ -(UITableViewCell *)tableView:(ZHCMessagesTableView *)tableView cellForRowAtInde cell.cellTopLabel.attributedText = [tableView.dataSource tableView:tableView attributedTextForCellTopLabelAtIndexPath:indexPath]; cell.messageBubbleTopLabel.attributedText = [tableView.dataSource tableView:tableView attributedTextForMessageBubbleTopLabelAtIndexPath:indexPath]; cell.cellBottomLabel.attributedText = [tableView.dataSource tableView:tableView attributedTextForCellBottomLabelAtIndexPath:indexPath]; - - CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath]; + CGFloat cellsSpaceLabelHeight = [tableView.dataSource tableView:tableView tableViewCellSeparatorHeightAtIndexpath:indexPath]; + CGFloat cellTopLabelHeight = [tableView.dataSource tableView:tableView heightForCellTopLabelAtIndexPath:indexPath]; CGFloat cellBubbleTopLabelHeight = [tableView.dataSource tableView:tableView heightForMessageBubbleTopLabelAtIndexPath:indexPath]; CGFloat cellBottomLabelHeight = [tableView.dataSource tableView:tableView heightForCellBottomLabelAtIndexPath:indexPath]; + cell.cellsSpaceLabelHeight = cellsSpaceLabelHeight; cell.cellTopLabelHeight = cellTopLabelHeight; cell.messageBubbleTopLabelHeight = cellBubbleTopLabelHeight; cell.cellBottomLabelHeight = cellBottomLabelHeight; @@ -708,6 +624,17 @@ - (NSAttributedString *)tableView:(ZHCMessagesTableView *)tableView attributedTe } #pragma mark - Adjusting cell label heights + +-(CGFloat)tableView:(ZHCMessagesTableView *)tableView tableViewCellSeparatorHeightAtIndexpath:(NSIndexPath *)indexPath +{ + if (indexPath.row == 0) { + return 5.0f; + }else{ + return kZHCMessagesTableViewCellSpaceDefault; + } +} + + -(CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath { return 0.0f; @@ -878,7 +805,6 @@ - (void)zhc_updateTableViewInsets - (void)zhc_setTableViewInsetsTopValue:(CGFloat)top bottomValue:(CGFloat)bottom { bottom = bottom +10; - NSLog(@"Bottom Value:%f",bottom); UIEdgeInsets insets = UIEdgeInsetsMake(top, 0.0f, bottom, 0.0f); self.messageTableView.contentInset = insets; self.messageTableView.scrollIndicatorInsets = insets; diff --git a/ZHCMessagesViewController/Layout/ZHCMessagesBubbleCalculator.m b/ZHCMessagesViewController/Layout/ZHCMessagesBubbleCalculator.m index 52ab1bd..9b7b625 100644 --- a/ZHCMessagesViewController/Layout/ZHCMessagesBubbleCalculator.m +++ b/ZHCMessagesViewController/Layout/ZHCMessagesBubbleCalculator.m @@ -78,13 +78,11 @@ -(CGSize)messageBubbleSizeForMessageData:(id)messageData atIndex { NSValue *cachedSize = [self.cache objectForKey:@([messageData messageHash])]; if (cachedSize != nil) { - NSLog(@"hit cache Size"); return [cachedSize CGSizeValue]; } CGSize finalSize = CGSizeZero; if ([messageData isMediaMessage]) { finalSize = [[messageData media] mediaViewDisplaySize]; - NSLog(@"mediaSize:width:%f---height:%f",finalSize.width,finalSize.height); }else{ ZHCMessagesTableviewLayoutAttributes *attributes = tableView.tableViewLayout; diff --git a/ZHCMessagesViewController/Models/ZHCMessagesTableViewDataSource.h b/ZHCMessagesViewController/Models/ZHCMessagesTableViewDataSource.h index 1a6b555..8aa8257 100644 --- a/ZHCMessagesViewController/Models/ZHCMessagesTableViewDataSource.h +++ b/ZHCMessagesViewController/Models/ZHCMessagesTableViewDataSource.h @@ -133,7 +133,7 @@ NS_ASSUME_NONNULL_BEGIN * Asks the delegate for the height of the `messageBubbleTopLabel` for the cell at the specified indexPath. * * @param tableView The tableView object displaying the flow layout. - * @param indexPath The index path of the cell. + * @param indexPath The index path of the cell. * * @return The height of the `messageBubbleTopLabel` for the cell at indexPath. * @@ -142,11 +142,21 @@ NS_ASSUME_NONNULL_BEGIN - (CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForMessageBubbleTopLabelAtIndexPath:(NSIndexPath *)indexPath; +/** + * Asks the delegate for the height of the `cellsSpaceLabel` for the cell at the specified indexPath. + * + * @param tableView The tableView object displaying the flow layout. + * @param indexPath The index path of the cell. + * + * @return The height of the `cellsSpaceLabel` for the cell at indexPath. + */ +-(CGFloat)tableView:(ZHCMessagesTableView *)tableView tableViewCellSeparatorHeightAtIndexpath:(NSIndexPath *)indexPath; + /** * Asks the delegate for the height of the `cellBottomLabel` for the cell at the specified indexPath. * * @param tableView The tableView object displaying the flow layout. - * @param indexPath The index path of the cell. + * @param indexPath The index path of the cell. * * @return The height of the `cellBottomLabel` for the cell at indexPath. * diff --git a/ZHCMessagesViewController/Views/ZHCMessagesInputToolbar.m b/ZHCMessagesViewController/Views/ZHCMessagesInputToolbar.m index 81343bc..0727d0a 100644 --- a/ZHCMessagesViewController/Views/ZHCMessagesInputToolbar.m +++ b/ZHCMessagesViewController/Views/ZHCMessagesInputToolbar.m @@ -101,7 +101,6 @@ - (void)setPreferredDefaultHeight:(CGFloat)preferredDefaultHeight */ -(void)zhc_startRecordVoice:(UIButton *)sender { - NSLog(@"startRecord"); sender.highlighted = YES; [ZHCMessagesAudioProgressHUD zhc_show]; [_recorder zhc_startRecording]; @@ -112,7 +111,6 @@ -(void)zhc_startRecordVoice:(UIButton *)sender */ -(void)zhc_cancelRecordVoice:(UIButton *)sender { - NSLog(@"cancelRecord"); sender.highlighted = NO; [ZHCMessagesAudioProgressHUD zhc_dismissWithMessage:[NSBundle zhc_localizedStringForKey:@"Cancel_Recording"]]; [_recorder zhc_cancelRecord]; @@ -123,7 +121,6 @@ -(void)zhc_cancelRecordVoice:(UIButton *)sender */ -(void)zhc_confirmRecordVoice:(UIButton *)sender { - NSLog(@"confirmRecord"); sender.highlighted = NO; [_recorder zhc_stopRecording]; } diff --git a/ZHCMessagesViewController/Views/ZHCMessagesTableView.m b/ZHCMessagesViewController/Views/ZHCMessagesTableView.m index be1e40c..f937872 100644 --- a/ZHCMessagesViewController/Views/ZHCMessagesTableView.m +++ b/ZHCMessagesViewController/Views/ZHCMessagesTableView.m @@ -41,6 +41,7 @@ -(void)awakeFromNib { [super awakeFromNib]; [self zhc_configureTableView]; + } - (void)zhc_configureTableView @@ -50,6 +51,7 @@ - (void)zhc_configureTableView self.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; self.alwaysBounceVertical = YES; self.bounces = YES; + self.tableHeaderView = [UIView new]; [self registerNib:[ZHCMessagesTableViewCellIncoming nib] forCellReuseIdentifier:[ZHCMessagesTableViewCellIncoming cellReuseIdentifier]]; [self registerNib:[ZHCMessagesTableViewCellOutcoming nib] forCellReuseIdentifier:[ZHCMessagesTableViewCellOutcoming cellReuseIdentifier]]; diff --git a/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.h b/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.h index e6fa9e8..616436f 100644 --- a/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.h +++ b/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.h @@ -94,6 +94,15 @@ NS_ASSUME_NONNULL_BEGIN @property (weak, nonatomic) id delegate; + +/** + * The height of the `cellsSpaceLabel` of a `ZHCMessagesTableviewCell`. + * This value should be greater than or equal to `0.0`. + * + * @see ZHCMessagesTableviewCell. + */ +@property (assign, nonatomic) CGFloat cellsSpaceLabelHeight; + /** * The height of the `cellTopLabel` of a `ZHCMessagesTableviewCell`. * This value should be greater than or equal to `0.0`. diff --git a/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.m b/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.m index d9ea710..9debf23 100644 --- a/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.m +++ b/ZHCMessagesViewController/Views/ZHCMessagesTableViewCell.m @@ -279,6 +279,23 @@ - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector #pragma mark - Setters + +-(void)setCellsSpaceLabelHeight:(CGFloat)cellsSpaceLabelHeight +{ + NSParameterAssert(cellsSpaceLabelHeight >= 0.0f); + if (cellsSpaceLabelHeight == 0.0f) { + self.cellsSpaceLabel.text = nil; + } + if (self.cellsSpaceConstraint.constant == cellsSpaceLabelHeight) { + return; + } + _cellsSpaceLabelHeight = cellsSpaceLabelHeight; + [self zhc_updateConstraint:self.cellsSpaceConstraint withConstant:cellsSpaceLabelHeight]; + +} + + + -(void)setCellTopLabelHeight:(CGFloat)cellTopLabelHeight { NSParameterAssert(cellTopLabelHeight >= 0.0f); @@ -311,7 +328,7 @@ -(void)setCellBottomLabelHeight:(CGFloat)cellBottomLabelHeight { NSParameterAssert(cellBottomLabelHeight >= 0.0f); if (cellBottomLabelHeight == 0.0f) { - self.cellTopLabel.text = nil; + self.cellBottomLabel.text = nil; } if (self.cellBottomLabelHeightConstraint.constant == cellBottomLabelHeight) { return; diff --git a/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.h b/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.h index eaeedca..169ec88 100644 --- a/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.h +++ b/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.h @@ -11,7 +11,7 @@ -@interface ZHCDemoMessagesViewController : ZHCMessagesViewController +@interface ZHCDemoMessagesViewController : ZHCMessagesViewController @property (strong, nonatomic) ZHCModelData *demoData; @property (assign, nonatomic) BOOL presentBool; @end diff --git a/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.m b/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.m index 0155876..d7dedf6 100644 --- a/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.m +++ b/ZHChat/ZHCMessagesDemo/ZHCDemoMessagesViewController.m @@ -68,7 +68,6 @@ -(NSString *)senderId -(void)tableView:(ZHCMessagesTableView *)tableView didDeleteMessageAtIndexPath:(NSIndexPath *)indexPath { - NSLog(@"didDeleteMessageAtIndexPath:%ld",(long)indexPath.row); [self.demoData.messages removeObjectAtIndex:indexPath.row]; } @@ -214,22 +213,18 @@ -(CGFloat)tableView:(ZHCMessagesTableView *)tableView heightForCellBottomLabelA -(void)tableView:(ZHCMessagesTableView *)tableView didTapAvatarImageView:(UIImageView *)avatarImageView atIndexPath:(NSIndexPath *)indexPath { [super tableView:tableView didTapAvatarImageView:avatarImageView atIndexPath:indexPath]; - NSLog(@"didTapAvatarImageViewIndexPath:%ld",(long)indexPath.row); } -(void)tableView:(ZHCMessagesTableView *)tableView didTapMessageBubbleAtIndexPath:(NSIndexPath *)indexPath { [super tableView:tableView didTapMessageBubbleAtIndexPath:indexPath]; - NSLog(@"didTapMessageBubbleAtIndexPath:%ld",(long)indexPath.row); - } -(void)tableView:(ZHCMessagesTableView *)tableView didTapCellAtIndexPath:(NSIndexPath *)indexPath touchLocation:(CGPoint)touchLocation { [super tableView:tableView didTapCellAtIndexPath:indexPath touchLocation:touchLocation]; - NSLog(@"didTapCellAtIndexPath:%ld",(long)indexPath.row); } @@ -371,14 +366,6 @@ -(NSArray *)messagesMoreViewImgNames:(ZHCMessagesMoreView *)moreView } - -#pragma mark - UIActionSheetDelegate - -- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex -{ - NSLog(@"click ActionSheet Button At index :%ld",(long)buttonIndex); -} - #pragma mark - PrivateMethods -(void)closePressed:(id)sender {