TUISystemMessageCell.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // TUISystemMessageCell.m
  3. // UIKit
  4. //
  5. // Created by annidyfeng on 2019/5/30.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUISystemMessageCell.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. #import <TUICore/NSString+TUIUtil.h>
  11. @interface TUISystemMessageCell ()
  12. @property(nonatomic, strong) UILabel *messageLabel;
  13. @property TUISystemMessageCellData *systemData;
  14. @end
  15. @implementation TUISystemMessageCell
  16. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  17. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  18. if (self) {
  19. _messageLabel = [[UILabel alloc] init];
  20. _messageLabel.textAlignment = NSTextAlignmentCenter;
  21. _messageLabel.numberOfLines = 0;
  22. _messageLabel.backgroundColor = [UIColor clearColor];
  23. _messageLabel.layer.cornerRadius = 3;
  24. [_messageLabel.layer setMasksToBounds:YES];
  25. [self.container addSubview:_messageLabel];
  26. self.backgroundColor = [UIColor clearColor];
  27. self.contentView.backgroundColor = [UIColor clearColor];
  28. }
  29. return self;
  30. }
  31. + (BOOL)requiresConstraintBasedLayout {
  32. return YES;
  33. }
  34. // this is Apple's recommended place for adding/updating constraints
  35. - (void)updateConstraints {
  36. [super updateConstraints];
  37. [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
  38. make.center.mas_equalTo(self.contentView);
  39. make.size.mas_equalTo(self.contentView);
  40. }];
  41. [self.messageLabel sizeToFit];
  42. if(self.messageLabel.superview) {
  43. [self.messageLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  44. make.center.mas_equalTo(self.container);
  45. make.leading.trailing.mas_equalTo(self.container);
  46. }];
  47. }
  48. }
  49. - (void)fillWithData:(TUISystemMessageCellData *)data;
  50. {
  51. [super fillWithData:data];
  52. self.systemData = data;
  53. self.messageLabel.textColor = TUISystemMessageCellData.textColor ? : data.contentColor;
  54. self.messageLabel.font = TUISystemMessageCellData.textFont ? : data.contentFont;
  55. self.messageLabel.backgroundColor = TUISystemMessageCellData.textBackgroundColor ? : [UIColor clearColor];
  56. self.messageLabel.attributedText = data.attributedString;
  57. self.nameLabel.hidden = YES;
  58. self.avatarView.hidden = YES;
  59. self.retryView.hidden = YES;
  60. [self.indicator stopAnimating];
  61. // tell constraints they need updating
  62. [self setNeedsUpdateConstraints];
  63. // update constraints now so we can animate the change
  64. [self updateConstraintsIfNeeded];
  65. [self layoutIfNeeded];
  66. }
  67. - (void)layoutSubviews {
  68. [super layoutSubviews];
  69. }
  70. #pragma mark - TUIMessageCellProtocol
  71. + (CGFloat)getEstimatedHeight:(TUIMessageCellData *)data {
  72. return 42.f;
  73. }
  74. + (CGFloat)getHeight:(TUIMessageCellData *)data withWidth:(CGFloat)width {
  75. return [self getContentSize:data].height + kScale375(16);
  76. }
  77. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  78. NSAssert([data isKindOfClass:TUISystemMessageCellData.class], @"data must be kind of TUISystemMessageCellData");
  79. TUISystemMessageCellData *systemCellData = (TUISystemMessageCellData *)data;
  80. static CGSize maxSystemSize;
  81. if (CGSizeEqualToSize(maxSystemSize, CGSizeZero)) {
  82. maxSystemSize = CGSizeMake(Screen_Width, MAXFLOAT);
  83. }
  84. CGSize size = [systemCellData.attributedString.string textSizeIn:maxSystemSize font:systemCellData.contentFont];
  85. size.height += 10;
  86. size.width += 16;
  87. return size;
  88. }
  89. @end