TXGroupNoticeMessageCell.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(249);
  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(249);
  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(249);
  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 setLineSpacing:4];//调整行间距
  56. CGFloat height = [noticeMsg.msgContent boundingRectWithSize:CGSizeMake(225, CGFLOAT_MAX) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size.height + 1;
  57. CGFloat contentHeight = height + 64 + 11;
  58. return contentHeight;
  59. }
  60. else {
  61. return 64 + 11;
  62. }
  63. }
  64. - (TXGroupNoticeMessageContentView *)noticeContentView {
  65. if (!_noticeContentView) {
  66. _noticeContentView = [TXGroupNoticeMessageContentView shareIntance];
  67. }
  68. return _noticeContentView;
  69. }
  70. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  71. [super setSelected:selected animated:animated];
  72. // Configure the view for the selected state
  73. }
  74. #pragma mark - TUIMessageCellProtocol
  75. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  76. TXGroupNoticeMessage *noticeMsg = (TXGroupNoticeMessage *)data;
  77. CGFloat width = ([UIScreen mainScreen].bounds.size.width > [UIScreen mainScreen].bounds.size.height ? [UIScreen mainScreen].bounds.size.height : [UIScreen mainScreen].bounds.size.width);
  78. if (![self isEmptyString:noticeMsg.msgContent]) {
  79. CGFloat height = [noticeMsg.msgContent boundingRectWithSize:CGSizeMake(225, CGFLOAT_MAX) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} context:nil].size.height + 6;
  80. CGFloat contentHeight = height + 64 + 11;
  81. return CGSizeMake(width, contentHeight);
  82. }
  83. else {
  84. return CGSizeMake(width, 64 + 11);
  85. }
  86. }
  87. + (BOOL)isEmptyString:(NSString *)str {
  88. if (str == nil || str.length == 0) {
  89. return YES;
  90. }
  91. return NO;
  92. }
  93. @end