JXSystemImage2Cell.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // JXSystemImage2Cell.m
  3. // shiku_im
  4. //
  5. // Created by p on 2017/7/20.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXSystemImage2Cell.h"
  9. @interface JXSystemImage2Cell ()
  10. @property (nonatomic, strong) UIImageView *imageBackground;
  11. @property (nonatomic, strong) UILabel *title;
  12. @property (nonatomic, strong) UIView *subViews;
  13. @property (nonatomic, strong) UIImageView *imageV;
  14. @property (nonatomic, strong) UIView *lineView;
  15. @end
  16. @implementation JXSystemImage2Cell
  17. -(void)creatUI{
  18. _imageBackground =[[UIImageView alloc]initWithFrame:CGRectZero];
  19. _imageBackground.userInteractionEnabled = YES;
  20. [_imageBackground setBackgroundColor:[UIColor clearColor]];
  21. _imageBackground.layer.cornerRadius = 6;
  22. _imageBackground.layer.masksToBounds = YES;
  23. [self.bubbleBg addSubview:_imageBackground];
  24. _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(18, 15, kSystemImageCellWidth - 30, 150)];
  25. _imageV.userInteractionEnabled = YES;
  26. UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
  27. [_imageV addGestureRecognizer:tap];
  28. [_imageBackground addSubview:_imageV];
  29. _title = [[UILabel alloc] initWithFrame:CGRectMake(0, _imageV.frame.size.height - 30, _imageV.frame.size.width, 30)];
  30. _title.font = SYSFONT(15.0);
  31. _title.backgroundColor = [UIColor colorWithWhite:0 alpha:.8];
  32. _title.textColor = [UIColor whiteColor];
  33. _title.numberOfLines = 0;
  34. _title.text = Localized(@"JXSystemImage_multiple");
  35. [_imageV addSubview:_title];
  36. _lineView = [[UIView alloc] initWithFrame:CGRectMake(18, CGRectGetMaxY(_imageV.frame) + 15, kSystemImageCellWidth - 30, LINE_WH)];
  37. _lineView.backgroundColor = THE_LINE_COLOR;
  38. [_imageBackground addSubview:_lineView];
  39. _subViews = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_lineView.frame), kSystemImageCellWidth, 0)];
  40. [_imageBackground addSubview:_subViews];
  41. }
  42. -(void)setCellData{
  43. [super setCellData];
  44. SBJsonParser * parser = [[SBJsonParser alloc] init] ;
  45. NSArray *content = [parser objectWithString:self.msg.content];
  46. if (content.count <= 0) {
  47. return;
  48. }
  49. NSDictionary *dict = [content objectAtIndex:0];
  50. // [_imageV sd_setImageWithURL:[dict objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"Default_Gray" ]];
  51. [_imageV sd_setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"img"]] placeholderImage:[UIImage imageNamed:@"Default_Gray"] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  52. if (error) {
  53. NSLog(@"error = %@",error);
  54. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  55. NSData *data = [NSData dataWithContentsOfURL:imageURL];
  56. UIImage *image = [UIImage imageWithData:data];
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. _imageV.image = image;
  59. });
  60. });
  61. }
  62. }];
  63. _title.text = [dict objectForKey:@"title"];
  64. CGSize size = [_title.text boundingRectWithSize:CGSizeMake(_title.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:SYSFONT(15.0)} context:nil].size;
  65. _title.frame = CGRectMake(_title.frame.origin.x, _imageV.frame.size.height - size.height - 10, _title.frame.size.width, size.height + 10);
  66. [self creatSubViews:content];
  67. if(self.msg.isMySend)
  68. {
  69. self.bubbleBg.frame=CGRectMake(CGRectGetMinX(self.headImage.frame)- kSystemImageCellWidth - CHAT_WIDTH_ICON, INSETS, kSystemImageCellWidth, 180 + _subViews.frame.size.height);
  70. _imageBackground.frame = self.bubbleBg.bounds;
  71. _imageBackground.image = [[UIImage imageNamed:@"chat_bg_blue"]stretchableImageWithLeftCapWidth:stretch topCapHeight:stretch];
  72. }
  73. else
  74. {
  75. self.bubbleBg.frame=CGRectMake(CGRectGetMaxX(self.headImage.frame) + CHAT_WIDTH_ICON, INSETS2(self.msg.isGroup), kSystemImageCellWidth, 180 + _subViews.frame.size.height);
  76. _imageBackground.frame = self.bubbleBg.bounds;
  77. _imageBackground.image = [[UIImage imageNamed:@"chat_bg_white"]stretchableImageWithLeftCapWidth:stretch topCapHeight:stretch];
  78. }
  79. if (self.msg.isShowTime) {
  80. CGRect frame = self.bubbleBg.frame;
  81. frame.origin.y = self.bubbleBg.frame.origin.y + 40;
  82. self.bubbleBg.frame = frame;
  83. }
  84. [self setMaskLayer:_imageBackground];
  85. }
  86. - (void) creatSubViews:(NSArray *)array{
  87. [_subViews.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  88. _subViews.frame = CGRectMake(_subViews.frame.origin.x, _subViews.frame.origin.y, _subViews.frame.size.width, 0);
  89. for (NSInteger i = 1; i < array.count; i ++) {
  90. NSDictionary *dict = array[i];
  91. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, _subViews.frame.size.height, _subViews.frame.size.width, 70)];
  92. [_subViews addSubview:btn];
  93. btn.tag = i;
  94. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_subViews.frame.size.width - 65, 10, 50, 50)];
  95. // [imageView sd_setImageWithURL:[dict objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"Default_Gray" ]];
  96. // [imageView sd_setImageWithURL:[dict objectForKey:@"img"] placeholderImage:[UIImage imageNamed:@"Default_Gray" ] options:SDWebImageRetryFailed];
  97. [btn addSubview:imageView];
  98. [imageView sd_setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"img"]] placeholderImage:[UIImage imageNamed:@"Default_Gray"] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  99. if (error) {
  100. NSLog(@"error = %@",error);
  101. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  102. NSData *data = [NSData dataWithContentsOfURL:imageURL];
  103. UIImage *image = [UIImage imageWithData:data];
  104. dispatch_async(dispatch_get_main_queue(), ^{
  105. imageView.image = image;
  106. });
  107. });
  108. }
  109. }];
  110. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 0, _subViews.frame.size.width - 65 - 18, btn.frame.size.height)];
  111. label.text = [dict objectForKey:@"title"];
  112. label.numberOfLines = 0;
  113. label.font = SYSFONT(14.0);
  114. [btn addSubview:label];
  115. UIView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(18, btn.frame.size.height - LINE_WH, btn.frame.size.width - 30, LINE_WH)];
  116. lineView.backgroundColor = THE_LINE_COLOR;
  117. [btn addSubview:lineView];
  118. [btn addTarget:self action:@selector(subViewsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  119. _subViews.frame = CGRectMake(_subViews.frame.origin.x, _subViews.frame.origin.y, _subViews.frame.size.width, _subViews.frame.size.height + btn.frame.size.height);
  120. }
  121. }
  122. - (void) subViewsBtnAction:(UIButton *) btn {
  123. SBJsonParser * parser = [[SBJsonParser alloc] init] ;
  124. NSArray *content = [parser objectWithString:self.msg.content];
  125. [g_notify postNotificationName:kCellSystemImage2DidTouchNotifaction object:content[btn.tag]];
  126. }
  127. -(void)tapAction:(UITapGestureRecognizer *)tap
  128. {
  129. SBJsonParser * parser = [[SBJsonParser alloc] init] ;
  130. NSArray *content = [parser objectWithString:self.msg.content];
  131. [g_notify postNotificationName:kCellSystemImage2DidTouchNotifaction object:content.firstObject];
  132. }
  133. + (float)getChatCellHeight:(JXMessageObject *)msg {
  134. if ([msg.chatMsgHeight floatValue] > 1) {
  135. return [msg.chatMsgHeight floatValue];
  136. }
  137. float n = 0;
  138. SBJsonParser * parser = [[SBJsonParser alloc] init] ;
  139. NSArray *content = [parser objectWithString:msg.content];
  140. if (content.count <= 0) {
  141. return 0;
  142. }
  143. if (msg.isShowTime) {
  144. n = 190 + 70 * (content.count - 1) + 40 + INSETS;
  145. }else {
  146. n = 190 + 70 * (content.count - 1) + INSETS;
  147. }
  148. msg.chatMsgHeight = [NSString stringWithFormat:@"%f",n];
  149. if (!msg.isNotUpdateHeight) {
  150. [msg updateChatMsgHeight];
  151. }
  152. return n;
  153. }
  154. @end