WFCUMessageCellBase.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // MessageCellBase.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUMessageCellBase.h"
  9. #import "WFCUUtilities.h"
  10. #import "UIFont+YH.h"
  11. #import "UIColor+YH.h"
  12. #import "WFCUUtilities.h"
  13. @implementation WFCUMessageCellBase
  14. + (CGSize)sizeForCell:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  15. return CGSizeMake(width, 80);
  16. }
  17. + (CGFloat)hightForHeaderArea:(WFCUMessageModel *)msgModel {
  18. CGFloat offset;
  19. if (msgModel.showTimeLabel) {
  20. offset = 30;
  21. } else {
  22. offset = 5;
  23. }
  24. if (msgModel.lastReadMessage) {
  25. offset += 30;
  26. }
  27. return offset;
  28. }
  29. - (void)onTaped:(id)sender {
  30. [self.delegate didTapMessageCell:self withModel:self.model];
  31. }
  32. - (void)onDoubleTaped:(id)sender {
  33. if ([self.delegate respondsToSelector:@selector(didDoubleTapMessageCell:withModel:)]) {
  34. [self.delegate didDoubleTapMessageCell:self withModel:self.model];
  35. }
  36. }
  37. - (void)onLongPressed:(id)sender {
  38. if ([sender isKindOfClass:[UILongPressGestureRecognizer class]]) {
  39. UILongPressGestureRecognizer *recognizer = (UILongPressGestureRecognizer *)sender;
  40. if(recognizer.state == UIGestureRecognizerStateBegan) {
  41. [self.delegate didLongPressMessageCell:self withModel:self.model];
  42. }
  43. }
  44. }
  45. - (void)setModel:(WFCUMessageModel *)model {
  46. CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
  47. _model = model;
  48. CGFloat offset = 5;
  49. if (model.lastReadMessage) {
  50. if (!self.lastReadContainerView) {
  51. self.lastReadContainerView = [[UIView alloc] initWithFrame:CGRectMake(16, offset, screenWidth-32, 20)];
  52. UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  53. label.text = WFCString(@"last_read_here");
  54. label.font = [UIFont systemFontOfSize:16];
  55. label.textAlignment = NSTextAlignmentCenter;
  56. label.textColor = [UIColor grayColor];
  57. label.layer.cornerRadius = 5.f;
  58. label.layer.masksToBounds = YES;
  59. CGSize size = [WFCUUtilities getTextDrawingSize:label.text font:label.font constrainedSize:CGSizeMake(screenWidth-16, 8000)];
  60. size.width += 16;
  61. label.frame = CGRectMake((screenWidth - 32 - size.width)/2, (20-size.height)/2, size.width, size.height);
  62. [self.lastReadContainerView addSubview:label];
  63. UIView *leftline = [[UIView alloc] initWithFrame:CGRectMake(0, 10, (screenWidth-32-size.width)/2-8, 1)];
  64. leftline.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.f];
  65. [self.lastReadContainerView addSubview:leftline];
  66. UIView *rightline = [[UIView alloc] initWithFrame:CGRectMake((screenWidth-32 + size.width)/2 + 8, 10, (screenWidth-32-size.width)/2-8, 1)];
  67. rightline.backgroundColor = leftline.backgroundColor;
  68. [self.lastReadContainerView addSubview:rightline];
  69. [self.contentView addSubview:self.lastReadContainerView];
  70. }
  71. offset += 20;
  72. } else {
  73. [self.lastReadContainerView removeFromSuperview];
  74. self.lastReadContainerView = nil;
  75. }
  76. if (model.showTimeLabel) {
  77. if (self.timeLabel == nil) {
  78. self.timeLabel = [[UILabel alloc] init];
  79. _timeLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:14];
  80. _timeLabel.textColor = [UIColor colorWithHexString:@"0xb3b3b3"];
  81. [self.contentView addSubview:self.timeLabel];
  82. }
  83. _timeLabel.hidden = NO;
  84. _timeLabel.text = [WFCUUtilities formatTimeDetailLabel:model.message.serverTime];
  85. CGSize size = [WFCUUtilities getTextDrawingSize:_timeLabel.text font:_timeLabel.font constrainedSize:CGSizeMake(screenWidth, 8000)];
  86. CGRect rect = CGRectMake((screenWidth - size.width)/2, offset + 5, size.width, size.height);
  87. _timeLabel.frame = rect;
  88. } else {
  89. _timeLabel.hidden = YES;
  90. }
  91. }
  92. @end