TUIEvaluationCell.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // TUIEvaluationCell.m
  3. // TUIChat
  4. //
  5. // Created by xia on 2022/6/10.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIEvaluationCell.h"
  9. #import <TUICore/TUIGlobalization.h>
  10. #import <TUICore/TUIThemeManager.h>
  11. @implementation TUIEvaluationCell
  12. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  13. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  14. if (self) {
  15. _titleLabel = [[UILabel alloc] init];
  16. _titleLabel.font = [UIFont systemFontOfSize:15];
  17. _titleLabel.numberOfLines = 1;
  18. _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  19. _titleLabel.textColor = TUIChatDynamicColor(@"chat_text_message_receive_text_color", @"#000000");
  20. [self.container addSubview:_titleLabel];
  21. for (int i = 0; i < 5; i++) {
  22. UIImageView *imageView = [[UIImageView alloc] init];
  23. [imageView setImage:TUIChatBundleThemeImage(@"chat_custom_evaluation_message_img", @"message_custom_evaluation")];
  24. [self.container addSubview:imageView];
  25. [self.starImageArray addObject:imageView];
  26. }
  27. _commentLabel = [[UILabel alloc] init];
  28. _commentLabel.font = [UIFont systemFontOfSize:15];
  29. _commentLabel.numberOfLines = 0;
  30. _commentLabel.textColor = TUIChatDynamicColor(@"chat_custom_evaluation_message_desc_color", @"#000000");
  31. [self.container addSubview:_commentLabel];
  32. }
  33. return self;
  34. }
  35. - (void)fillWithData:(TUIEvaluationCellData *)data {
  36. [super fillWithData:data];
  37. self.titleLabel.text = data.desc;
  38. self.commentLabel.text = data.comment;
  39. // Configure all StarViews to avoid UI cache clutter
  40. for (int i = 0; i < self.starImageArray.count; i++) {
  41. UIImageView *starView = [self.starImageArray objectAtIndex:i];
  42. starView.hidden = (i >= data.score);
  43. }
  44. // tell constraints they need updating
  45. [self setNeedsUpdateConstraints];
  46. // update constraints now so we can animate the change
  47. [self updateConstraintsIfNeeded];
  48. [self layoutIfNeeded];
  49. }
  50. + (BOOL)requiresConstraintBasedLayout {
  51. return YES;
  52. }
  53. // this is Apple's recommended place for adding/updating constraints
  54. - (void)updateConstraints {
  55. [super updateConstraints];
  56. [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  57. make.top.mas_equalTo(10);
  58. make.leading.mas_equalTo(10);
  59. make.width.mas_equalTo(225);
  60. make.height.mas_equalTo(18);
  61. }];
  62. UIImageView *leftView = nil;
  63. for (UIImageView *starView in self.starImageArray) {
  64. if (leftView == nil) {
  65. [starView mas_remakeConstraints:^(MASConstraintMaker *make) {
  66. make.leading.mas_equalTo(10);
  67. make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(6);
  68. make.width.mas_equalTo(30);
  69. make.height.mas_equalTo(30);
  70. }];
  71. } else {
  72. [starView mas_remakeConstraints:^(MASConstraintMaker *make) {
  73. make.leading.mas_equalTo(leftView.mas_trailing);
  74. make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(6);
  75. make.width.mas_equalTo(30);
  76. make.height.mas_equalTo(30);
  77. }];
  78. }
  79. leftView = starView;
  80. }
  81. UIImageView *starView = self.starImageArray.firstObject;
  82. self.commentLabel.hidden = self.commentLabel.text.length == 0;
  83. if (self.commentLabel.text.length > 0) {
  84. CGRect rect = [self.commentLabel.text boundingRectWithSize:CGSizeMake(225, MAXFLOAT)
  85. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  86. attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}
  87. context:nil];
  88. CGSize size = CGSizeMake(225, ceilf(rect.size.height));
  89. [self.commentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  90. make.top.mas_equalTo(starView.mas_bottom).mas_offset(6);
  91. make.leading.mas_equalTo(10);
  92. make.width.mas_equalTo(size.width);
  93. make.height.mas_equalTo(size.height);
  94. }];
  95. }
  96. }
  97. - (void)layoutSubviews {
  98. [super layoutSubviews];
  99. }
  100. - (NSMutableArray *)starImageArray {
  101. if (!_starImageArray) {
  102. _starImageArray = [[NSMutableArray alloc] init];
  103. }
  104. return _starImageArray;
  105. }
  106. #pragma mark - TUIMessageCellProtocol
  107. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  108. NSAssert([data isKindOfClass:TUIEvaluationCellData.class], @"data must be kind of TUIEvaluationCellData");
  109. TUIEvaluationCellData *evaluationCellData = (TUIEvaluationCellData *)data;
  110. CGRect rect = [evaluationCellData.comment boundingRectWithSize:CGSizeMake(215, MAXFLOAT)
  111. options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
  112. attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}
  113. context:nil];
  114. CGSize size = CGSizeMake(245, ceilf(rect.size.height));
  115. size.height += evaluationCellData.comment.length > 0 ? 88 : 50;
  116. return size;
  117. }
  118. @end