YOUPAILCMessageTxtContentView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // YOUPAILCMessageTxtContentView.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/22.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCMessageTxtContentView.h"
  9. #import "YOUPAILCMessageTxtAttachment.h"
  10. #import "NIMKitInfoFetchOption.h"
  11. @interface YOUPAILCMessageTxtContentView()
  12. @property(nonatomic,strong)UIView* backView;
  13. @property(nonatomic,strong)UILabel* topTitle;
  14. @property(nonatomic,strong)UILabel* timeTitle;
  15. @property(nonatomic,strong)UIView* botmView;
  16. @property(nonatomic,assign)CGFloat currentY;
  17. @end
  18. @implementation YOUPAILCMessageTxtContentView
  19. - (instancetype)initSessionMessageContentView{
  20. self = [super initSessionMessageContentView];
  21. if (self) {
  22. self.opaque = YES;
  23. UIView* testView = [[UIView alloc] initWithFrame:CGRectZero];
  24. testView.backgroundColor = [UIColor clearColor];
  25. UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandel:)];
  26. [testView addGestureRecognizer:tap];
  27. self.backView = testView;
  28. self.bubbleImageView.hidden = YES;
  29. [self addSubview:testView];
  30. // UILabel* topTitle = [[UILabel alloc]initWithFrame:CGRectMake(14.0f, 0, KScreenWidth-28.0f, 20)];
  31. // self.topTitle = topTitle;
  32. // topTitle.font = [UIFont systemFontOfSize:18];
  33. // topTitle.textColor = LCTextBlack;
  34. // [self.backView addSubview:topTitle];
  35. //
  36. //// UILabel* timeTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, KScreenWidth-70, 20)];
  37. //// self.timeTitle = timeTitle;
  38. //// timeTitle.font = [UIFont systemFontOfSize:15];
  39. //// timeTitle.textColor = LCTextGray;
  40. //// [self.backView addSubview:timeTitle];
  41. //
  42. // UIView* botmView = [[UIView alloc]initWithFrame:CGRectZero];
  43. // botmView.backgroundColor = [UIColor clearColor];
  44. // self.botmView = botmView;
  45. // botmView.hidden = YES;
  46. // [self.backView addSubview:botmView];
  47. // UIView* lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-28, 0.5)];
  48. // lineView.backgroundColor = HexColorFromRGB(0x28262D);
  49. // [botmView addSubview:lineView];
  50. //
  51. // UILabel* btnLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-70, 44)];
  52. // btnLabel.textAlignment = NSTextAlignmentLeft;
  53. // btnLabel.font = [UIFont systemFontOfSize:12];
  54. // btnLabel.textColor = HexColorFromRGB(0x9F9DA5);
  55. // btnLabel.text = @"详情 >";
  56. // [botmView addSubview:btnLabel];
  57. }
  58. return self;
  59. }
  60. - (void)refresh:(NIMMessageModel *)data{
  61. [super refresh:data];
  62. NIMCustomObject *customObject = (NIMCustomObject*)data.message.messageObject;
  63. id attachment = customObject.attachment;
  64. if ([attachment isKindOfClass:[YOUPAILCMessageTxtAttachment class]]) {
  65. for (UIView* subView in self.backView.subviews) {
  66. [subView removeFromSuperview];
  67. }
  68. YOUPAILCMessageTxtAttachment* attachment = (YOUPAILCMessageTxtAttachment*)customObject.attachment;
  69. UIImageView *avatarImgV = [[UIImageView alloc] init];
  70. avatarImgV.layer.cornerRadius = 10.0f;
  71. avatarImgV.clipsToBounds = YES;
  72. avatarImgV.contentMode = UIViewContentModeScaleAspectFill;
  73. [self setAvatarBySession:data.message.session avatarImgV:avatarImgV] ;
  74. [self.backView addSubview:avatarImgV];
  75. [avatarImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.offset(16.0f);
  77. make.size.mas_offset(CGSizeMake(40.0f, 40.0f));
  78. make.top.offset(0.0f);
  79. }];
  80. UIView *bgV = [[UIView alloc] init];
  81. bgV.backgroundColor = [UIColor whiteColor];
  82. bgV.layer.cornerRadius = 10.0f;
  83. bgV.clipsToBounds = YES;
  84. [self.backView addSubview:bgV];
  85. [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
  86. make.left.equalTo(avatarImgV.mas_right).offset(10.0f);
  87. make.width.offset(243.0f);
  88. make.top.bottom.offset(0.0f);
  89. }];
  90. UILabel* topTitle = [[UILabel alloc]init];
  91. topTitle.numberOfLines = 0;
  92. // self.topTitle = topTitle;
  93. // CGFloat topTitleHeight = [attachment.title heightWithFont:LCFont16 padding:3.0 constrainedToWidth:KScreenWidth-28.0f];
  94. topTitle.text = attachment.title;
  95. topTitle.font = LCFont(14.0f);
  96. topTitle.textColor = LZ273145Color;
  97. // topTitle.mj_h = topTitleHeight+2;
  98. [UILabel changeLineSpaceForLabel:topTitle WithSpace:3.0];
  99. [bgV addSubview:topTitle];
  100. [topTitle mas_makeConstraints:^(MASConstraintMaker *make) {
  101. make.left.offset(12.0f);
  102. make.right.offset(-12.0f);
  103. make.top.offset(10.0f);
  104. }];
  105. UIView *frontV = topTitle;
  106. //
  107. //// UILabel* timeTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, topTitleHeight+25, KScreenWidth-70, 20)];
  108. //// self.timeTitle = timeTitle;
  109. //// timeTitle.font = [UIFont systemFontOfSize:15];
  110. //// timeTitle.textColor = LCTextGray;
  111. //// self.timeTitle.text = attachment.date;
  112. //// [self.backView addSubview:timeTitle];
  113. // self.currentY = topTitleHeight + 17.0f;
  114. //
  115. UIView* botmView = [[UIView alloc]init];
  116. botmView.backgroundColor = [UIColor clearColor];
  117. // self.botmView = botmView;
  118. botmView.hidden = YES;
  119. [bgV addSubview:botmView];
  120. [botmView mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.left.right.bottom.offset(0.0f);
  122. make.height.offset(32.0f);
  123. }];
  124. [botmView addLineWithColor:LZF5F4F7Color lineRect:CGRectMake(12.0f, 0.0f, 243.0f - 24.0f, 0.5f)];
  125. //
  126. UILabel* btnLabel = [[UILabel alloc]init];
  127. btnLabel.textAlignment = NSTextAlignmentLeft;
  128. btnLabel.font = [UIFont systemFontOfSize:12];
  129. btnLabel.textColor = LZ7C69FEColor;
  130. btnLabel.text = @"详情 >";
  131. [botmView addSubview:btnLabel];
  132. [btnLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.left.offset(12.0f);
  134. make.right.offset(-12.0f);
  135. make.top.bottom.offset(0.0f);
  136. }];
  137. //
  138. if (![attachment.txt1 isEqualToString:@""]) {
  139. UILabel* txtOneLabel = [[UILabel alloc]init];
  140. txtOneLabel.numberOfLines = 0;
  141. txtOneLabel.text = attachment.txt1;
  142. txtOneLabel.font = LCFont(11.0f);
  143. txtOneLabel.textColor = LZA3AABEColor;
  144. // CGFloat txtOneHeight =[attachment.txt1 heightWithFont:LCFont12 padding:3.0 constrainedToWidth:KScreenWidth-28.0f];
  145. // txtOneLabel.frame = CGRectMake(14.0f, self.currentY, KScreenWidth-28.0f, txtOneHeight+2);
  146. [UILabel changeLineSpaceForLabel:txtOneLabel WithSpace:3.0];
  147. [bgV addSubview:txtOneLabel];
  148. [txtOneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  149. make.left.offset(12.0f);
  150. make.right.offset(-12.0f);
  151. make.top.equalTo(frontV.mas_bottom).offset(5.0f);
  152. }];
  153. frontV = txtOneLabel;
  154. // self.currentY += txtOneHeight;
  155. }
  156. if ([attachment.fields isKindOfClass:[NSArray class]] &&attachment.fields.count>0) {
  157. // self.currentY += 5.0f;
  158. for (NSDictionary* dict in attachment.fields) {
  159. NSString* name = [dict objectForKey:@"n"];
  160. NSString* value = [dict objectForKey:@"v"];
  161. UILabel* dataLabel = [[UILabel alloc]init];
  162. NSString* dataStr = [NSString stringWithFormat:@"%@: %@",name,value];
  163. // CGFloat dataLabelHeight =[dataStr heightWithFont:LCFont12 padding:3.0 constrainedToWidth:KScreenWidth-28.0f];
  164. dataLabel.numberOfLines = 0;
  165. dataLabel.font = LCFont(11.0f);
  166. dataLabel.textColor = LZA3AABEColor;
  167. dataLabel.text = dataStr;
  168. // dataLabel.frame = CGRectMake(14.0f, self.currentY, KScreenWidth-28.0f, dataLabelHeight+2);
  169. [UILabel changeLineSpaceForLabel:dataLabel WithSpace:3.0];
  170. [bgV addSubview:dataLabel];
  171. [dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  172. make.left.offset(12.0f);
  173. make.right.offset(-12.0f);
  174. make.top.equalTo(frontV.mas_bottom).offset(5.0f);
  175. }];
  176. frontV = dataLabel;
  177. // self.currentY +=dataLabelHeight+5;
  178. }
  179. }
  180. if (![attachment.txt2 isEqualToString:@""]) {
  181. UILabel* txtTwoLabel = [[UILabel alloc]init];
  182. txtTwoLabel.text = attachment.txt2;
  183. txtTwoLabel.numberOfLines = 0;
  184. txtTwoLabel.font = LCFont(11.0f);
  185. txtTwoLabel.textColor = LZA3AABEColor;
  186. // CGFloat txtTwoHeight =[attachment.txt2 heightWithFont:LCFont12 padding:3.0 constrainedToWidth:KScreenWidth-28.0f];
  187. // txtTwoLabel.frame = CGRectMake(14.0f, self.currentY+14.0f, KScreenWidth-28.0f, txtTwoHeight+2);
  188. [UILabel changeLineSpaceForLabel:txtTwoLabel WithSpace:3.0];
  189. [bgV addSubview:txtTwoLabel];
  190. [txtTwoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  191. make.left.offset(12.0f);
  192. make.right.offset(-12.0f);
  193. make.top.equalTo(frontV.mas_bottom).offset(5.0f);
  194. }];
  195. frontV = txtTwoLabel;
  196. // self.currentY += txtTwoHeight+16;
  197. }
  198. // self.currentY += 17.0f;
  199. if (attachment.link_type!=0) {
  200. botmView.hidden = NO;
  201. }
  202. // [self.backView addLineWithColor:HexColorFromRGB(0x28262D) lineRect:CGRectMake(14.0f, self.currentY - 0.5f, KScreenWidth - 28.0f, 0.5f)];
  203. }
  204. }
  205. - (void)tapHandel:(UITapGestureRecognizer*)tap{
  206. if (tap.state == UIGestureRecognizerStateEnded) {
  207. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  208. event.eventName = NIMKitEventNameTapContent;
  209. event.messageModel = self.model;
  210. [self.delegate onCatchEvent:event];
  211. }
  212. }
  213. - (void)onTouchUpInside:(id)sender
  214. {
  215. NIMKitEvent *event = [[NIMKitEvent alloc] init];
  216. event.eventName = NIMKitEventNameTapContent;
  217. event.messageModel = self.model;
  218. [self.delegate onCatchEvent:event];
  219. }
  220. - (void)layoutSubviews{
  221. [super layoutSubviews];
  222. UIEdgeInsets contentInsets = self.model.contentViewInsets;
  223. CGSize contentsize = [self.model contentSize:KScreenWidth];
  224. CGRect imageViewFrame = CGRectMake(contentInsets.left, contentInsets.top, contentsize.width, contentsize.height);
  225. self.backView.frame = imageViewFrame;
  226. }
  227. - (void)setAvatarBySession:(NIMSession *)session avatarImgV:(UIImageView *)avatarImgV
  228. {
  229. NIMKitInfo *info = nil;
  230. if (session.sessionType == NIMSessionTypeTeam)
  231. {
  232. info = [[NIMKit sharedKit] infoByTeam:session.sessionId option:nil];
  233. }
  234. else
  235. {
  236. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  237. option.session = session;
  238. info = [[NIMKit sharedKit] infoByUser:session.sessionId option:option];
  239. }
  240. NSURL *url = info.avatarUrlString ? [NSURL URLWithString:info.avatarUrlString] : nil;
  241. NIMUser* nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:session.sessionId];
  242. if(nimUser.userInfo.gender == NIMUserGenderMale){//默认男头像
  243. info.avatarImage = [UIImage imageNamed:@"woman"];
  244. }else{
  245. info.avatarImage = [UIImage imageNamed:@"vqu_images_P_man"];
  246. }
  247. [avatarImgV sd_setImageWithURL:[[LCTools getImageUrlWithAddress:[url absoluteString]] urlWithImageScale:60] placeholderImage:info.avatarImage];
  248. }
  249. @end