WFCUInformationCell.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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] addObserverForName:kUserInfoUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
  39. WFCCUserInfo *userInfo = note.userInfo[@"userInfo"];
  40. BOOL needUpdate = false;
  41. if ([ws.model.message.content isKindOfClass:[WFCCAddGroupeMemberNotificationContent class]]) {
  42. WFCCAddGroupeMemberNotificationContent *cnt = (WFCCAddGroupeMemberNotificationContent *)ws.model.message.content;
  43. if ([cnt.invitor isEqualToString:userInfo.userId] || [cnt.invitees containsObject:userInfo.userId]) {
  44. needUpdate = true;
  45. }
  46. } else if ([ws.model.message.content isKindOfClass:[WFCCCreateGroupNotificationContent class]]) {
  47. WFCCCreateGroupNotificationContent *cnt = (WFCCCreateGroupNotificationContent *)ws.model.message.content;
  48. if ([cnt.creator isEqualToString:userInfo.userId]) {
  49. needUpdate = true;
  50. }
  51. }
  52. if (needUpdate) {
  53. [ws setModel:ws.model];
  54. }
  55. }];
  56. NSString *infoText;
  57. if ([model.message.content isKindOfClass:[WFCCNotificationMessageContent class]]) {
  58. WFCCNotificationMessageContent *content = (WFCCNotificationMessageContent *)model.message.content;
  59. infoText = [content formatNotification:model.message];
  60. } else {
  61. infoText = [model.message digest];
  62. }
  63. CGFloat width = self.contentView.bounds.size.width;
  64. 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)];
  65. self.infoLabel.text = infoText;
  66. self.infoLabel.layoutMargins = UIEdgeInsetsMake(TEXT_TOP_PADDING, TEXT_LEFT_PADDING, TEXT_BUTTOM_PADDING, TEXT_RIGHT_PADDING);
  67. CGFloat timeLableEnd = 0;
  68. if (!self.timeLabel.hidden) {
  69. timeLableEnd = self.timeLabel.frame.size.height + self.timeLabel.frame.origin.y;
  70. }
  71. 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);
  72. // self.infoLabel.textAlignment = NSTextAlignmentCenter;
  73. }
  74. - (UILabel *)infoLabel {
  75. if (!_infoLabel) {
  76. _infoLabel = [[UILabel alloc] init];
  77. _infoLabel.numberOfLines = 0;
  78. _infoLabel.font = [UIFont systemFontOfSize:14];
  79. _infoLabel.textColor = [UIColor whiteColor];
  80. _infoLabel.numberOfLines = 0;
  81. _infoLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  82. _infoLabel.textAlignment = NSTextAlignmentCenter;
  83. _infoLabel.font = [UIFont systemFontOfSize:14.f];
  84. _infoLabel.layer.masksToBounds = YES;
  85. _infoLabel.layer.cornerRadius = 5.f;
  86. _infoLabel.textAlignment = NSTextAlignmentCenter;
  87. _infoLabel.backgroundColor = [UIColor colorWithRed:201/255.f green:201/255.f blue:201/255.f alpha:1.f];
  88. [self.contentView addSubview:_infoLabel];
  89. }
  90. return _infoLabel;
  91. }
  92. - (void)dealloc {
  93. [[NSNotificationCenter defaultCenter] removeObserver:self];
  94. }
  95. @end