TXGroupNoticeMessageCell.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // TXGroupNoticeMessageCell.m
  3. // TUIChat
  4. //
  5. // Created by 王智 on 2024/8/30.
  6. //
  7. #import "TXGroupNoticeMessageCell.h"
  8. #import "TXGroupNoticeMessageContentView.h"
  9. #import "Masonry.h"
  10. @interface TXGroupNoticeMessageCell ()
  11. @property (nonatomic, strong) TXGroupNoticeMessageContentView *noticeContentView;
  12. @end
  13. @implementation TXGroupNoticeMessageCell
  14. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  15. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  16. if (self) {
  17. [self configUI];
  18. }
  19. return self;
  20. }
  21. - (void)configUI {
  22. [self.container addSubview:self.noticeContentView];
  23. [self.noticeContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  24. make.left.mas_equalTo(self.avatarView.mas_right).offset(10);
  25. make.top.mas_equalTo(self.avatarView.mas_top);
  26. make.width.mas_equalTo(245);
  27. make.bottom.mas_equalTo(self.container.mas_bottom).offset(-10);
  28. }];
  29. }
  30. - (void)fillWithData:(TXGroupNoticeMessage *)data {
  31. [super fillWithData:data];
  32. [self.noticeContentView configCellWithMsgTitle:data.msgTitle content:data.msgContent];
  33. CGFloat height = [self getContentViewHeight:data];
  34. if (data.direction == MsgDirectionIncoming) {
  35. [self.noticeContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  36. make.left.mas_equalTo(self.avatarView.mas_right).offset(10);
  37. make.top.mas_equalTo(self.avatarView.mas_top);
  38. make.width.mas_equalTo(245);
  39. make.height.mas_equalTo(height);
  40. }];
  41. }
  42. else {
  43. [self.noticeContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
  44. make.right.mas_equalTo(self.avatarView.mas_left).offset(-10);
  45. make.top.mas_equalTo(self.avatarView.mas_top);
  46. make.width.mas_equalTo(245);
  47. make.height.mas_equalTo(height);
  48. }];
  49. }
  50. }
  51. - (CGFloat)getContentViewHeight:(TUIMessageCellData *)data {
  52. TXGroupNoticeMessage *noticeMsg = (TXGroupNoticeMessage *)data;
  53. if (![TXGroupNoticeMessageCell isEmptyString:noticeMsg.msgContent]) {
  54. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  55. paragraphStyle.maximumLineHeight = 21;
  56. paragraphStyle.minimumLineHeight = 21;
  57. UIFont *font = [UIFont systemFontOfSize:16.0f];
  58. CGFloat height = [noticeMsg.msgContent boundingRectWithSize:CGSizeMake(221, CGFLOAT_MAX) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font} context:nil].size.height + 1;
  59. CGFloat contentHeight = height + 64 + 11;
  60. return contentHeight;
  61. }
  62. else {
  63. return 64 + 11;
  64. }
  65. }
  66. - (TXGroupNoticeMessageContentView *)noticeContentView {
  67. if (!_noticeContentView) {
  68. _noticeContentView = [TXGroupNoticeMessageContentView shareIntance];
  69. }
  70. return _noticeContentView;
  71. }
  72. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  73. [super setSelected:selected animated:animated];
  74. // Configure the view for the selected state
  75. }
  76. #pragma mark - TUIMessageCellProtocol
  77. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  78. TXGroupNoticeMessage *noticeMsg = (TXGroupNoticeMessage *)data;
  79. CGFloat width = ([UIScreen mainScreen].bounds.size.width > [UIScreen mainScreen].bounds.size.height ? [UIScreen mainScreen].bounds.size.height : [UIScreen mainScreen].bounds.size.width);
  80. if (![self isEmptyString:noticeMsg.msgContent]) {
  81. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  82. paragraphStyle.maximumLineHeight = 21;
  83. paragraphStyle.minimumLineHeight = 21;
  84. UIFont *font = [UIFont systemFontOfSize:16.0f];
  85. CGFloat height = [noticeMsg.msgContent boundingRectWithSize:CGSizeMake(221, CGFLOAT_MAX) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size.height + 6;
  86. CGFloat contentHeight = height + 64 + 11;
  87. return CGSizeMake(width, contentHeight);
  88. }
  89. else {
  90. return CGSizeMake(width, 64 + 11);
  91. }
  92. }
  93. + (BOOL)isEmptyString:(NSString *)str {
  94. if (str == nil || str.length == 0) {
  95. return YES;
  96. }
  97. return NO;
  98. }
  99. @end