TUIFaceMessageCell.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // FaceMessageCell.m
  3. // UIKit
  4. //
  5. // Created by annidyfeng on 2019/5/30.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIFaceMessageCell.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. @interface TUIFaceMessageCell ()
  11. @end
  12. @implementation TUIFaceMessageCell
  13. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  14. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  15. if (self) {
  16. _face = [[UIImageView alloc] init];
  17. _face.contentMode = UIViewContentModeScaleAspectFit;
  18. [self.container addSubview:_face];
  19. _face.mm_fill();
  20. _face.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  21. }
  22. return self;
  23. }
  24. - (void)layoutSubviews {
  25. [super layoutSubviews];
  26. }
  27. + (BOOL)requiresConstraintBasedLayout {
  28. return YES;
  29. }
  30. // this is Apple's recommended place for adding/updating constraints
  31. - (void)updateConstraints {
  32. [super updateConstraints];
  33. CGFloat topMargin = 0;
  34. CGFloat height = self.container.mm_h;
  35. if (self.messageData.messageContainerAppendSize.height > 0) {
  36. topMargin = 10;
  37. CGFloat tagViewTopPadding = 6;
  38. height = self.container.mm_h - topMargin - self.messageData.messageContainerAppendSize.height - tagViewTopPadding;
  39. self.bubbleView.hidden = NO;
  40. } else {
  41. self.bubbleView.hidden = YES;
  42. }
  43. [self.face mas_remakeConstraints:^(MASConstraintMaker *make) {
  44. make.height.mas_equalTo(height);
  45. make.centerX.mas_equalTo(self.container.mas_centerX);
  46. make.top.mas_equalTo(topMargin);
  47. make.width.mas_equalTo(self.container);
  48. }];
  49. }
  50. - (void)fillWithData:(TUIFaceMessageCellData *)data {
  51. // set data
  52. [super fillWithData:data];
  53. self.faceData = data;
  54. UIImage *image = [[TUIImageCache sharedInstance] getFaceFromCache:data.path];
  55. if (!image) {
  56. image = [UIImage imageWithContentsOfFile:TUIChatFaceImagePath(@"ic_unknown_image")];
  57. }
  58. _face.image = image;
  59. // tell constraints they need updating
  60. [self setNeedsUpdateConstraints];
  61. // update constraints now so we can animate the change
  62. [self updateConstraintsIfNeeded];
  63. [self layoutIfNeeded];
  64. }
  65. #pragma mark - TUIMessageCellProtocol
  66. + (CGSize)getContentSize:(TUIMessageCellData *)data {
  67. NSAssert([data isKindOfClass:TUIFaceMessageCellData.class], @"data must be kind of TUIFaceMessageCellData");
  68. TUIFaceMessageCellData *faceCellData = (TUIFaceMessageCellData *)data;
  69. UIImage *image = [[TUIImageCache sharedInstance] getFaceFromCache:faceCellData.path];
  70. if (!image) {
  71. image = [UIImage imageWithContentsOfFile:TUIChatFaceImagePath(@"ic_unknown_image")];
  72. }
  73. CGFloat imageHeight = image.size.height;
  74. CGFloat imageWidth = image.size.width;
  75. if (imageHeight > TFaceMessageCell_Image_Height_Max) {
  76. imageHeight = TFaceMessageCell_Image_Height_Max;
  77. imageWidth = image.size.width / image.size.height * imageHeight;
  78. }
  79. if (imageWidth > TFaceMessageCell_Image_Width_Max) {
  80. imageWidth = TFaceMessageCell_Image_Width_Max;
  81. imageHeight = image.size.height / image.size.width * imageWidth;
  82. }
  83. return CGSizeMake(imageWidth, imageHeight);
  84. }
  85. @end