WFCUCompositeBaseCell.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // WFCUCompositeBaseCell.m
  3. // WFChatUIKit
  4. //
  5. // Created by Tom Lee on 2020/10/4.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUCompositeBaseCell.h"
  9. #import "WFCUCompositeTextCell.h"
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import <SDWebImage/SDWebImage.h>
  12. @interface WFCUCompositeBaseCell ()
  13. @property(nonatomic, strong)UIImageView *portraitImageView;
  14. @property(nonatomic, strong)UILabel *nameLabel;
  15. @property(nonatomic, strong)UILabel *timeLabel;
  16. @property(nonatomic, strong)UIView *line;
  17. @end
  18. @implementation WFCUCompositeBaseCell
  19. - (void)awakeFromNib {
  20. [super awakeFromNib];
  21. // Initialization code
  22. }
  23. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  24. [super setSelected:selected animated:animated];
  25. // Configure the view for the selected state
  26. }
  27. + (instancetype)cellOfMessage:(WFCCMessage *)message {
  28. if ([message.content isKindOfClass:[WFCCTextMessageContent class]]) {
  29. WFCUCompositeTextCell *cell = [[WFCUCompositeTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([message.content class])];
  30. for (UIView *view in cell.contentView.subviews) {
  31. [view removeFromSuperview];
  32. }
  33. return cell;
  34. }
  35. return nil;
  36. }
  37. + (CGFloat)heightForMessage:(WFCCMessage *)message {
  38. return COMPOSITE_CELL_TOP_PADDING + COMPOSITE_CELL_NAME_LABEL_HEIGHT + COMPOSITE_CELL_NAME_CONTENT_PADDING + [self heightForMessageContent:message] + COMPOSITE_CELL_BUTTOM_PADDING + COMPOSITE_CELL_LINE_HEIGHT;
  39. }
  40. + (CGFloat)heightForMessageContent:(WFCCMessage *)message {
  41. return 0;
  42. }
  43. + (CGRect)contentFrame {
  44. CGFloat x = COMPOSITE_CELL_PORTRAIT_PADDING + COMPOSITE_CELL_PORTRAIT_WIDTH + COMPOSITE_CELL_PORTRAIT_PADDING;
  45. CGFloat y = COMPOSITE_CELL_TOP_PADDING+COMPOSITE_CELL_NAME_LABEL_HEIGHT+COMPOSITE_CELL_NAME_CONTENT_PADDING;
  46. CGFloat w = [UIScreen mainScreen].bounds.size.width - x - COMPOSITE_CELL_RIGHT_PADDING;
  47. return CGRectMake(x, y, w, 0);
  48. }
  49. - (void)setMessage:(WFCCMessage *)message {
  50. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:message.fromUser refresh:NO];
  51. if (self.hiddenPortrait) {
  52. _portraitImageView.hidden = YES;
  53. } else {
  54. self.portraitImageView.hidden = NO;
  55. [self.portraitImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.portrait] placeholderImage:[UIImage imageNamed:@"PersonlChat"]];
  56. }
  57. self.nameLabel.text = userInfo.displayName;
  58. NSDate *from = [[NSDate alloc] initWithTimeIntervalSince1970:message.serverTime/1000];
  59. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  60. [dateFormatter setDateFormat:@"MM-dd HH:mm"];
  61. [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
  62. self.timeLabel.text = [dateFormatter stringFromDate:from];
  63. CGFloat x;
  64. CGFloat cellHeight = [self.class heightForMessage:message];
  65. if (self.lastMessage) {
  66. x = COMPOSITE_CELL_RIGHT_PADDING;
  67. } else {
  68. x = COMPOSITE_CELL_PORTRAIT_PADDING + COMPOSITE_CELL_PORTRAIT_WIDTH + COMPOSITE_CELL_PORTRAIT_PADDING;
  69. }
  70. self.line.frame = CGRectMake(x, cellHeight-1, [UIScreen mainScreen].bounds.size.width-x-COMPOSITE_CELL_RIGHT_PADDING, 1);
  71. }
  72. - (UIImageView *)portraitImageView {
  73. if (!_portraitImageView) {
  74. _portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(COMPOSITE_CELL_PORTRAIT_PADDING, COMPOSITE_CELL_TOP_PADDING, COMPOSITE_CELL_PORTRAIT_WIDTH, COMPOSITE_CELL_PORTRAIT_WIDTH)];
  75. [self.contentView addSubview:_portraitImageView];
  76. }
  77. return _portraitImageView;
  78. }
  79. - (UILabel *)nameLabel {
  80. if (!_nameLabel) {
  81. CGFloat x = COMPOSITE_CELL_PORTRAIT_PADDING + COMPOSITE_CELL_PORTRAIT_WIDTH + COMPOSITE_CELL_PORTRAIT_PADDING;
  82. CGFloat w = [UIScreen mainScreen].bounds.size.width - x -
  83. - COMPOSITE_CELL_RIGHT_PADDING - COMPOSITE_CELL_TIME_LABEL_WIDTH - COMPOSITE_CELL_RIGHT_PADDING;
  84. _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, COMPOSITE_CELL_TOP_PADDING, w, COMPOSITE_CELL_NAME_LABEL_HEIGHT)];
  85. [_nameLabel setFont:[UIFont systemFontOfSize:COMPOSITE_CELL_NAME_LABEL_FONT]];
  86. _nameLabel.textColor = [UIColor grayColor];
  87. [self.contentView addSubview:_nameLabel];
  88. }
  89. return _nameLabel;
  90. }
  91. - (UILabel *)timeLabel {
  92. if (!_timeLabel) {
  93. CGFloat x = [UIScreen mainScreen].bounds.size.width - COMPOSITE_CELL_TIME_LABEL_WIDTH - COMPOSITE_CELL_RIGHT_PADDING;
  94. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, COMPOSITE_CELL_TOP_PADDING, COMPOSITE_CELL_TIME_LABEL_WIDTH, COMPOSITE_CELL_TIME_LABEL_HEIGHT)];
  95. [_timeLabel setFont:[UIFont systemFontOfSize:COMPOSITE_CELL_TIME_LABEL_FONT]];
  96. _timeLabel.textAlignment = NSTextAlignmentRight;
  97. _timeLabel.textColor = [UIColor grayColor];
  98. [self.contentView addSubview:_timeLabel];
  99. }
  100. return _timeLabel;
  101. }
  102. - (UIView *)line {
  103. if (!_line) {
  104. _line = [[UIView alloc] init];
  105. _line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.f];
  106. [self.contentView addSubview:_line];
  107. }
  108. return _line;
  109. }
  110. @end