WFCUInformationCell.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // InformationCell.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/9/1.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUInformationCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUUtilities.h"
  11. #define TEXT_TOP_PADDING 6
  12. #define TEXT_BUTTOM_PADDING 6
  13. #define TEXT_LEFT_PADDING 8
  14. #define TEXT_RIGHT_PADDING 8
  15. #define TEXT_LABEL_TOP_PADDING TEXT_TOP_PADDING + 4
  16. #define TEXT_LABEL_BUTTOM_PADDING TEXT_BUTTOM_PADDING + 4
  17. #define TEXT_LABEL_LEFT_PADDING 30
  18. #define TEXT_LABEL_RIGHT_PADDING 30
  19. @implementation WFCUInformationCell
  20. + (CGSize)sizeForCell:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
  21. CGFloat height = [super hightForHeaderArea:msgModel];
  22. NSString *infoText;
  23. if ([msgModel.message.content isKindOfClass:[WFCCNotificationMessageContent class]]) {
  24. WFCCNotificationMessageContent *content = (WFCCNotificationMessageContent *)msgModel.message.content;
  25. infoText = [content formatNotification:msgModel.message];
  26. } else {
  27. infoText = [msgModel.message digest];
  28. }
  29. CGSize size = [WFCUUtilities getTextDrawingSize:infoText font:[UIFont systemFontOfSize:14] constrainedSize:CGSizeMake(width - TEXT_LABEL_LEFT_PADDING - TEXT_LABEL_RIGHT_PADDING - TEXT_LEFT_PADDING - TEXT_RIGHT_PADDING, 8000)];
  30. size.height += TEXT_LABEL_TOP_PADDING + TEXT_LABEL_BUTTOM_PADDING + TEXT_TOP_PADDING + TEXT_BUTTOM_PADDING;
  31. size.height += height;
  32. return CGSizeMake(width, size.height);
  33. }
  34. - (void)setModel:(WFCUMessageModel *)model {
  35. [super setModel:model];
  36. __weak typeof(self)ws = self;
  37. [[NSNotificationCenter defaultCenter] removeObserver:self];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoUpdated:) name:kUserInfoUpdated object:nil];
  39. NSString *infoText;
  40. if ([model.message.content isKindOfClass:[WFCCNotificationMessageContent class]]) {
  41. WFCCNotificationMessageContent *content = (WFCCNotificationMessageContent *)model.message.content;
  42. infoText = [content formatNotification:model.message];
  43. } else {
  44. infoText = [model.message digest];
  45. }
  46. CGFloat width = self.contentView.bounds.size.width;
  47. CGSize size = [WFCUUtilities getTextDrawingSize:infoText font:[UIFont systemFontOfSize:14] constrainedSize:CGSizeMake(width - TEXT_LABEL_LEFT_PADDING - TEXT_LABEL_RIGHT_PADDING - TEXT_LEFT_PADDING - TEXT_RIGHT_PADDING, 8000)];
  48. self.infoLabel.text = infoText;
  49. self.infoLabel.layoutMargins = UIEdgeInsetsMake(TEXT_TOP_PADDING, TEXT_LEFT_PADDING, TEXT_BUTTOM_PADDING, TEXT_RIGHT_PADDING);
  50. CGFloat timeLableEnd = 0;
  51. if (!self.timeLabel.hidden) {
  52. timeLableEnd = self.timeLabel.frame.size.height + self.timeLabel.frame.origin.y;
  53. }
  54. self.infoLabel.frame = CGRectMake((width - size.width)/2 - 8, timeLableEnd + TEXT_LABEL_TOP_PADDING, size.width + 16, size.height + TEXT_TOP_PADDING + TEXT_BUTTOM_PADDING);
  55. // self.infoLabel.textAlignment = NSTextAlignmentCenter;
  56. }
  57. - (void)onUserInfoUpdated:(NSNotification *)notification {
  58. NSArray<WFCCUserInfo *> *userInfoList = notification.userInfo[@"userInfoList"];
  59. BOOL needUpdate = false;
  60. for (WFCCUserInfo *userInfo in userInfoList) {
  61. if ([self.model.message.content isKindOfClass:[WFCCAddGroupeMemberNotificationContent class]]) {
  62. WFCCAddGroupeMemberNotificationContent *cnt = (WFCCAddGroupeMemberNotificationContent *)self.model.message.content;
  63. if ([cnt.invitor isEqualToString:userInfo.userId] || [cnt.invitees containsObject:userInfo.userId]) {
  64. needUpdate = true;
  65. break;
  66. }
  67. } else if ([self.model.message.content isKindOfClass:[WFCCCreateGroupNotificationContent class]]) {
  68. WFCCCreateGroupNotificationContent *cnt = (WFCCCreateGroupNotificationContent *)self.model.message.content;
  69. if ([cnt.creator isEqualToString:userInfo.userId]) {
  70. needUpdate = true;
  71. break;
  72. }
  73. }
  74. }
  75. if (needUpdate) {
  76. [self setModel:self.model];
  77. }
  78. }
  79. - (UILabel *)infoLabel {
  80. if (!_infoLabel) {
  81. _infoLabel = [[UILabel alloc] init];
  82. _infoLabel.numberOfLines = 0;
  83. _infoLabel.font = [UIFont systemFontOfSize:14];
  84. _infoLabel.textColor = [UIColor whiteColor];
  85. _infoLabel.numberOfLines = 0;
  86. _infoLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  87. _infoLabel.textAlignment = NSTextAlignmentCenter;
  88. _infoLabel.font = [UIFont systemFontOfSize:14.f];
  89. _infoLabel.layer.masksToBounds = YES;
  90. _infoLabel.layer.cornerRadius = 5.f;
  91. _infoLabel.textAlignment = NSTextAlignmentCenter;
  92. _infoLabel.backgroundColor = [UIColor colorWithRed:201/255.f green:201/255.f blue:201/255.f alpha:1.f];
  93. [self.contentView addSubview:_infoLabel];
  94. }
  95. return _infoLabel;
  96. }
  97. - (void)dealloc {
  98. [[NSNotificationCenter defaultCenter] removeObserver:self];
  99. }
  100. @end