MediaMessageGridViewCell.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // MediaMessageGridViewCell.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/7/1.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "MediaMessageGridViewCell.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "WFCUImage.h"
  11. @interface MediaMessageGridViewCell ()
  12. @property(nonatomic, strong)UIImageView *imageView;
  13. @property(nonatomic, strong)UIImageView *videoFlag;
  14. @property(nonatomic, strong)UILabel *videoDuration;
  15. @property(nonatomic, strong)UILabel *fileName;
  16. @property(nonatomic, strong)UILabel *fileSize;
  17. @end
  18. @implementation MediaMessageGridViewCell
  19. -(void)setMediaMessage:(WFCCMessage *)mediaMessage {
  20. _mediaMessage = mediaMessage;
  21. if ([mediaMessage.content isKindOfClass:[WFCCImageMessageContent class]]) {
  22. WFCCImageMessageContent *imgCnt = (WFCCImageMessageContent *)mediaMessage.content;
  23. self.imageView.image = imgCnt.thumbnail;
  24. self.imageView.hidden = NO;
  25. self.videoFlag.hidden = YES;
  26. self.videoDuration.hidden = YES;
  27. self.fileSize.hidden = YES;
  28. self.fileName.hidden = YES;
  29. } else if([mediaMessage.content isKindOfClass:[WFCCVideoMessageContent class]]) {
  30. WFCCVideoMessageContent *videoCnt = (WFCCVideoMessageContent *)mediaMessage.content;
  31. self.imageView.image = videoCnt.thumbnail;
  32. if (videoCnt.duration == 0) {
  33. self.videoDuration.text = nil;
  34. } else if(videoCnt.duration > 60) {
  35. self.videoDuration.text = [NSString stringWithFormat:@"%ld:%2ld", videoCnt.duration/60, videoCnt.duration%60];
  36. } else {
  37. self.videoDuration.text = [NSString stringWithFormat:@"0:%2ld", videoCnt.duration];
  38. }
  39. self.imageView.hidden = NO;
  40. self.videoFlag.hidden = NO;
  41. self.videoDuration.hidden = NO;
  42. self.fileSize.hidden = YES;
  43. self.fileName.hidden = YES;
  44. } else if([mediaMessage.content isKindOfClass:[WFCCFileMessageContent class]]) {
  45. WFCCFileMessageContent *fileCnt = (WFCCFileMessageContent *)mediaMessage.content;
  46. self.fileName.text = fileCnt.name;
  47. if (fileCnt.size > 1024 * 1024) {
  48. self.fileSize.text = [NSString stringWithFormat:@"%ldM", fileCnt.size/1024/1024];
  49. } else if(fileCnt.size > 1024) {
  50. self.fileSize.text = [NSString stringWithFormat:@"%ldK", fileCnt.size/1024];
  51. } else {
  52. self.fileSize.text = [NSString stringWithFormat:@"%ldB", fileCnt.size];
  53. }
  54. self.backgroundColor = [UIColor grayColor];
  55. self.imageView.hidden = YES;
  56. self.videoFlag.hidden = YES;
  57. self.videoDuration.hidden = YES;
  58. self.fileSize.hidden = NO;
  59. self.fileName.hidden = NO;
  60. }
  61. }
  62. - (UIImageView *)imageView {
  63. if (!_imageView) {
  64. _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  65. [self addSubview:_imageView];
  66. }
  67. return _imageView;
  68. }
  69. - (UIImageView *)videoFlag {
  70. if (!_videoFlag) {
  71. _videoFlag = [[UIImageView alloc] initWithFrame:CGRectMake(8, self.bounds.size.height-24, 15, 12)];
  72. _videoFlag.image = [WFCUImage imageNamed:@"video"];
  73. [self addSubview:_videoFlag];
  74. }
  75. return _videoFlag;
  76. }
  77. -(UILabel *)videoDuration {
  78. if (!_videoDuration) {
  79. _videoDuration = [[UILabel alloc] initWithFrame:CGRectMake(28, self.bounds.size.height-24-2, 40, 16)];
  80. _videoDuration.font = [UIFont systemFontOfSize:12];
  81. _videoDuration.lineBreakMode = NSLineBreakByTruncatingHead;
  82. [self addSubview:_videoDuration];
  83. }
  84. return _videoDuration;
  85. }
  86. -(UILabel *)fileName {
  87. if (!_fileName) {
  88. _fileName = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, self.bounds.size.width-16, 40)];
  89. _fileName.font = [UIFont systemFontOfSize:14];
  90. _fileName.lineBreakMode = NSLineBreakByTruncatingMiddle;
  91. _fileName.numberOfLines = 2;
  92. [self addSubview:_fileName];
  93. }
  94. return _fileName;
  95. }
  96. -(UILabel *)fileSize {
  97. if (!_fileSize) {
  98. _fileSize = [[UILabel alloc] initWithFrame:CGRectMake(8, self.bounds.size.height-24, self.bounds.size.width-16, 16)];
  99. _fileSize.font = [UIFont systemFontOfSize:14];
  100. _fileSize.lineBreakMode = NSLineBreakByTruncatingHead;
  101. [self addSubview:_fileSize];
  102. }
  103. return _fileSize;
  104. }
  105. @end