TCShowLiveMessageView.m 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. //
  2. // TCShowLiveMessageView.m
  3. // TCShow
  4. //
  5. // Created by AlexiChen on 16/4/14.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "TCShowLiveMessageView.h"
  9. #define LIVE_MESSAGEVIEW_Margin 10
  10. @implementation TCShowLiveMsgTableViewCell
  11. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  12. {
  13. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
  14. {
  15. self.selectionStyle = UITableViewCellSelectionStyleNone;
  16. self.backgroundColor = kClearColor;
  17. _type = @"0";
  18. _myFont = [UIFont systemFontOfSize:16.0];
  19. //聊天背景框
  20. _msgBgImgView = [[UIImageView alloc]init];
  21. _msgBgImgView.layer.cornerRadius = 7;
  22. _msgBgImgView.layer.borderColor = kClearColor.CGColor;
  23. // kBlueColor.CGColor;
  24. _msgBgImgView.layer.masksToBounds = YES;
  25. _msgBgImgView.layer.borderWidth = 1.0f;
  26. // _msgBgImgView.hidden = YES;
  27. [self.contentView addSubview:_msgBgImgView];
  28. //聊天背景view
  29. _msgBack = [[UIImageView alloc] init];
  30. // _msgBack.image = [UIImage imageNamed:@"bogo_liveroom_livemessviewBGimg_Normal"];
  31. _msgBack.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
  32. _msgBack.layer.cornerRadius = 6;
  33. _msgBack.layer.masksToBounds = YES;
  34. //
  35. // [UIColor colorWithWhite:0 alpha:0.2];
  36. // [UIColor colorWithWhite:0 alpha:0.2];
  37. //
  38. // kBlueColor;
  39. //
  40. _msgBack.layer.cornerRadius = 6;
  41. _msgBack.layer.masksToBounds = YES;
  42. [self.contentView addSubview:_msgBack];
  43. _msgLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  44. _msgLabel.numberOfLines = 0;
  45. _msgLabel.font = _myFont;
  46. // _msgLabel.delegate = self;
  47. _msgLabel.textAlignment = NSTextAlignmentLeft;
  48. _msgLabel.backgroundColor = [UIColor clearColor];
  49. _msgLabel.lineBreakMode = NSLineBreakByCharWrapping;
  50. // _msgLabel.disableThreeCommon = YES; //禁用电话,邮箱,连接三者
  51. // _msgLabel.disableEmoji = NO; //禁用表情
  52. [_msgBack addSubview:_msgLabel];
  53. _rankImgView = [[UIImageView alloc]init];
  54. // [_msgBack addSubview:_rankImgView];
  55. _rankImgView.hidden = YES;
  56. //vipView
  57. _vipView = [[UIImageView alloc]init];
  58. [_vipView setImage:[UIImage imageNamed:@"vip_icon_s"]];
  59. // [_msgBack addSubview:_vipView];
  60. _vipView.hidden = YES;
  61. _stealthView = [UIImageView new];
  62. // [_msgBack addSubview:_stealthView];
  63. _stealthView.hidden = YES;
  64. //守护View
  65. _guardianView = [[UIImageView alloc]init];
  66. // [_guardianView setImage:[UIImage imageNamed:@"lr_img_corner_ward"]];
  67. // [_msgBack addSubview:_guardianView];
  68. _guardianView.hidden = YES;
  69. //贵族View
  70. _nobleVipView = [[UIImageView alloc]init];
  71. // _nobleVipView.backgroundColor = k;
  72. // [_nobleVipView setImage:[UIImage imageNamed:@"lr_img_corner_ward"]];
  73. _nobleVipView.contentMode = UIViewContentModeScaleAspectFit;
  74. // [_msgBack addSubview:_nobleVipView];
  75. _nobleVipView.hidden = YES;
  76. }
  77. return self;
  78. }
  79. - (CAGradientLayer*)gradientLayerWithColor1:(UIColor*)color1 AtColor2:(UIColor*)color2 view:(UIView *)view
  80. {
  81. CAGradientLayer* layer = [CAGradientLayer new];
  82. layer.colors = @[ (__bridge id)color1.CGColor, (__bridge id)color2.CGColor];
  83. layer.startPoint = CGPointMake(0.5f, -0.5);
  84. layer.endPoint = CGPointMake(0.5, 1);
  85. layer.frame = view.bounds;
  86. return layer;
  87. }
  88. - (BOOL)isHostLive:(NSString *)currentUserId
  89. {
  90. return [[IMAPlatform sharedInstance].host.profile.identifier isEqualToString:currentUserId];
  91. }
  92. #pragma mark 设置聊天列表信息
  93. - (void)config:(CustomMessageModel *)item block:(FWVoidBlock)block
  94. {
  95. CustomMessageModel *customMessageModel = (CustomMessageModel *)item;
  96. NSString *iconImgWidthStr = @" ";
  97. if (customMessageModel.sender.is_guardian == 1) {
  98. //如果是守护,守护view显示
  99. _guardianView.hidden = NO;
  100. // iconImgWidthStr = [NSString stringWithFormat:@"%@%@"];
  101. }else{
  102. _guardianView.hidden = YES;
  103. }
  104. //
  105. if (customMessageModel.sender.noble_vip_type.intValue == 1) {
  106. _nobleVipView.hidden = NO;
  107. [_nobleVipView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.noble_icon]];
  108. // CAGradientLayer *layer = [self gradientLayerWithColor1:[UIColor colorWithHexString:@"#BC7B02"] AtColor2:[UIColor colorWithHexString:@"#E9BD15"] view:_msgBack];
  109. // [_msgBack.layer addSublayer:layer];
  110. }else{
  111. _nobleVipView.hidden = YES;
  112. // CAGradientLayer* layer = nil;
  113. // [_msgBack.layer addSublayer:layer];
  114. }
  115. if (customMessageModel.sender.is_vip.integerValue == 1) {
  116. //如果是Vip,vipView显示
  117. _vipView.hidden = NO;
  118. }else{
  119. _vipView.hidden = YES;
  120. }
  121. FWWeakify(self)
  122. if(customMessageModel.text.length == 0 && customMessageModel.desc.length == 0 && customMessageModel.desc2.length == 0 && customMessageModel.msg.length)
  123. {
  124. return;
  125. }
  126. _customMessageModel = customMessageModel;
  127. GlobalVariables *BuguLive = [GlobalVariables sharedInstance];
  128. if ([customMessageModel.sender.user_id isEqualToString:[[IMAPlatform sharedInstance].host imUserId]] && [[IMAPlatform sharedInstance].host getUserRank] < customMessageModel.sender.user_level)
  129. {
  130. [[IMAPlatform sharedInstance].host setUserRank:[NSString stringWithFormat:@"%ld",(long)customMessageModel.sender.user_level]];
  131. }
  132. NSInteger type = customMessageModel.type;
  133. __block UIColor *messageLabelColor;
  134. // 设置颜色
  135. if (customMessageModel.fonts_color.length>0)
  136. {
  137. NSMutableString *statusbar_color = [NSMutableString stringWithString:customMessageModel.fonts_color];
  138. if ([statusbar_color hasPrefix:@"#"])
  139. {
  140. [statusbar_color deleteCharactersInRange:NSMakeRange(0,1)];
  141. }
  142. unsigned int hexValue = 0;
  143. NSScanner *scanner = [NSScanner scannerWithString:statusbar_color];
  144. [scanner setScanLocation:0]; // depends on your exact string format you may have to use location 1
  145. [scanner scanHexInt:&hexValue];
  146. messageLabelColor = RGBOF(hexValue);;
  147. }
  148. else
  149. {
  150. if (type == MSG_TEXT || type == MSG_POP_MSG)
  151. {
  152. messageLabelColor = myTextColorCommonMessage;
  153. }
  154. else if(type == MSG_SEND_GIFT_SUCCESS)
  155. {
  156. messageLabelColor = myTextColorSendGift;
  157. }
  158. else if(type == MSG_LIGHT)
  159. {
  160. messageLabelColor = kTextColorSendLight;
  161. }
  162. else if (type == MSG_RED_PACKET)
  163. {
  164. messageLabelColor = myTextColorRedPackage;
  165. }
  166. else if (type == MSG_VIEWER_JOIN)
  167. {
  168. messageLabelColor = myTextColorLivingMessage;
  169. }
  170. else if (type == MSG_PAI_SUCCESS || type == MSG_PAI_PAY_TIP || type == MSG_PAI_FAULT || type == MSG_ADD_PRICE || type == MSG_PAY_SUCCESS || type == MSG_RELEASE_SUCCESS || type == MSG_STARGOODS_SUCCESS)
  171. {
  172. messageLabelColor = kAppSecondaryColor;
  173. }
  174. else
  175. {
  176. messageLabelColor = myTextColorLivingMessage;
  177. }
  178. }
  179. // 整条消息
  180. NSString *messageStr = @"";
  181. // 设置消息的前半部分显示的内容,当前只有两种形式:1、直播消息 2、等级图标+vip+守护+用户名
  182. NSString *nameStr = @""; //消息的名字(直播消息或者是用户名字)
  183. NSString *nickName = customMessageModel.sender.nick_name;
  184. if ([BGUtils isBlankString:nickName])
  185. {
  186. nickName = customMessageModel.user.nick_name;
  187. if ([BGUtils isBlankString:nickName])
  188. {
  189. nickName = @" ";
  190. }
  191. }
  192. if(type == MSG_FORBID_SEND_MSG || type == MSG_VIEWER_JOIN || type == MSG_LIVING_MESSAGE || type == MSG_ANCHOR_LEAVE || type == MSG_ANCHOR_BACK || type == MSG_STARGOODS_SUCCESS || type == MSG_RELEASE_SUCCESS)
  193. {
  194. //直播消息
  195. _rankImgView.hidden = YES;
  196. nameStr = ASLocalizedString(@"直播消息:");
  197. _vipView.hidden = YES;
  198. _stealthView.hidden = YES;
  199. _guardianView.hidden = YES;
  200. _nobleVipView.hidden = YES;
  201. }
  202. else if( type == MSG_PAI_SUCCESS || type == MSG_PAI_PAY_TIP || type == MSG_PAI_FAULT || type == MSG_ADD_PRICE || type == MSG_PAY_SUCCESS || type == MSG_BUYGOODS_SUCCESS || type == MSG_RELEASE_SUCCESS)
  203. {
  204. //4-15 苹果昵称和vip标签,vip标签把昵称挡住了。
  205. //竞拍消息 观众购物支付成功消息
  206. messageStr = [NSString stringWithFormat:@"%@",messageStr];
  207. _rankImgView.hidden = NO;
  208. //5-7 礼物的等级
  209. if (customMessageModel.sender.user_level)
  210. {
  211. [_rankImgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"level%ld",(long)customMessageModel.sender.user_level]]];
  212. }
  213. else
  214. {
  215. [_rankImgView setImage:[UIImage imageNamed:@"rank_1"]];
  216. }
  217. if (customMessageModel.sender.is_guardian == 1) {
  218. //如果是守护,守护view显示
  219. _guardianView.hidden = NO;
  220. }else{
  221. _guardianView.hidden = YES;
  222. }
  223. [_guardianView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.guardian_img]];
  224. if (customMessageModel.sender.is_vip.integerValue == 1) {
  225. //如果是Vip,vipView显示
  226. _vipView.hidden = NO;
  227. }else{
  228. _vipView.hidden = YES;
  229. }
  230. // if (customMessageModel.sender.noble_vip_type.intValue == 1) {
  231. // _nobleVipView.hidden = NO;
  232. // [_nobleVipView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.noble_icon]];
  233. // }else{
  234. // _nobleVipView.hidden = YES;
  235. // }
  236. if (!_guardianView.hidden && !_vipView.hidden) {
  237. messageStr = [NSString stringWithFormat:@"%@",messageStr];
  238. }else if (!_guardianView.hidden || !_vipView.hidden){
  239. messageStr = [NSString stringWithFormat:@"%@",messageStr];
  240. }
  241. nameStr = [NSString stringWithFormat:@"%@:", nickName];
  242. }
  243. else
  244. {
  245. // messageStr = [NSString stringWithFormat:@" %@",messageStr];
  246. NSInteger space = 0;
  247. if (customMessageModel.sender.user_level)
  248. {
  249. [_rankImgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"level%ld",(long)customMessageModel.sender.user_level]]];
  250. }
  251. else
  252. {
  253. [_rankImgView setImage:[UIImage imageNamed:@"rank_1"]];
  254. }
  255. _rankImgView.hidden = NO;
  256. if (customMessageModel.sender.is_vip.integerValue == 1) {
  257. //如果是Vip,vipView显示
  258. _vipView.hidden = NO;
  259. }else{
  260. _vipView.hidden = YES;
  261. }
  262. nameStr = [NSString stringWithFormat:@"%@:", nickName];
  263. }
  264. // 拼接消息的前半部分
  265. messageStr = [messageStr stringByAppendingString:nameStr];
  266. // 设置消息的后半部分显示的内容
  267. NSString *contentStr = @"";
  268. if (type == MSG_TEXT)
  269. {
  270. contentStr = customMessageModel.text;
  271. }
  272. else if (type == MSG_POP_MSG)
  273. {
  274. contentStr = customMessageModel.desc;
  275. }
  276. else if (type == MSG_SEND_GIFT_SUCCESS || type == MSG_RED_PACKET)
  277. {
  278. if ([self isHostLive:customMessageModel.sender.imUserId])
  279. {
  280. contentStr = customMessageModel.desc2;
  281. }
  282. else
  283. {
  284. contentStr = customMessageModel.desc;
  285. }
  286. }
  287. else if (type == MSG_ANCHOR_LEAVE || type == MSG_ANCHOR_BACK)
  288. {
  289. contentStr = customMessageModel.text;
  290. }
  291. else if (type == MSG_LIGHT)
  292. {
  293. contentStr = ASLocalizedString(@"我点亮了");
  294. }
  295. else if (type == MSG_VIEWER_JOIN)
  296. {
  297. if (customMessageModel.sender.user_level >= BuguLive.appModel.jr_user_level)
  298. {
  299. contentStr = [NSString stringWithFormat:ASLocalizedString(@"金光一闪,%@ 加入了..."),nickName];
  300. }
  301. else
  302. {
  303. contentStr = [NSString stringWithFormat:ASLocalizedString(@"%@ 来了"),nickName];
  304. }
  305. if ([[GlobalVariables sharedInstance].appModel.open_noble isEqualToString:@"1"]){
  306. if ([customMessageModel.sender.is_noble_mysterious isEqualToString:@"1"]) {
  307. contentStr = ASLocalizedString(@"神秘人加入了房间");
  308. }else{
  309. // messageStr = [messageStr stringByAppendingString:[NSString stringWithFormat:@"%@", contentStr]];
  310. }
  311. }
  312. }
  313. else if (type == MSG_PAI_SUCCESS || type == MSG_PAI_PAY_TIP || type == MSG_PAI_FAULT || type == MSG_ADD_PRICE || type == MSG_PAY_SUCCESS )
  314. {
  315. if (customMessageModel.desc)
  316. {
  317. contentStr = customMessageModel.desc;
  318. }
  319. }
  320. else
  321. {
  322. if (customMessageModel.desc)
  323. {
  324. contentStr = customMessageModel.desc;
  325. }
  326. else if (customMessageModel.text)
  327. {
  328. contentStr = customMessageModel.text;
  329. }
  330. else
  331. {
  332. contentStr = @"";
  333. }
  334. }
  335. NSLog(@"%@333333%@",messageStr,contentStr);
  336. if ([[GlobalVariables sharedInstance].appModel.open_noble isEqualToString:@"1"]){
  337. if ([customMessageModel.sender.is_noble_mysterious isEqualToString:@"1"]) {
  338. NSLog(ASLocalizedString(@"是隐身了"));
  339. nameStr = @"";
  340. messageStr = [NSString stringWithFormat:@"%@",contentStr];
  341. _nobleVipView.hidden = _rankImgView.hidden = _vipView.hidden = _guardianView.hidden = YES;
  342. _stealthView.hidden = NO;
  343. [_stealthView setImage:[UIImage imageNamed:@"live_noble_Img"]];
  344. [_msgBgImgView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.star_box]];
  345. // _msgBgImgView.hidden = NO;
  346. }else{
  347. messageStr = [messageStr stringByAppendingString:[NSString stringWithFormat:@"%@", contentStr]];
  348. }
  349. }else{
  350. _stealthView.hidden = YES;
  351. // 整条消息拼接,message不可能为空,如果为空就意味着判断出错了
  352. if (contentStr && ![contentStr isEqualToString:@""])
  353. {
  354. messageStr = [messageStr stringByAppendingString:[NSString stringWithFormat:@"%@", contentStr]];
  355. }
  356. }
  357. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:messageStr];
  358. self.imageCount = (_nobleVipView.isHidden ? 0 : 1)*2+(_rankImgView.isHidden ? 0 : 2)*2+(_guardianView.isHidden ? 0 : 1)*2+(_stealthView.isHidden ? 0 : 1)*2;
  359. if (!_stealthView.isHidden) {
  360. NSAttributedString *spaceAttr = [[NSAttributedString alloc]initWithString:@" "];
  361. NSTextAttachment *text = [[NSTextAttachment alloc]init];
  362. text.image = [UIImage imageNamed:@"live_noble_Img"];
  363. text.bounds = CGRectMake(0, -3.5, 50, 18);
  364. NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:text];
  365. [attr insertAttributedString:spaceAttr atIndex:0];
  366. [attr insertAttributedString:imageAttr atIndex:0];
  367. }
  368. if (!_nobleVipView.isHidden) {
  369. UIImage *nobleImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:customMessageModel.sender.noble_icon];
  370. NSAttributedString *spaceAttr = [[NSAttributedString alloc]initWithString:@" "];
  371. NSTextAttachment *text = [[NSTextAttachment alloc]init];
  372. if (nobleImage) {
  373. text.image = nobleImage;
  374. text.bounds = CGRectMake(0, -3.5, nobleImage.size.width * 18 / nobleImage.size.height, 18);
  375. }else{
  376. text.image = [[UIImage alloc]init];
  377. text.bounds = CGRectMake(0, -3.5, 1, 1);
  378. }
  379. NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:text];
  380. [attr insertAttributedString:spaceAttr atIndex:0];
  381. [attr insertAttributedString:imageAttr atIndex:0];
  382. if (!nobleImage) {
  383. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:customMessageModel.sender.noble_icon] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  384. NSTextAttachment *nText = [[NSTextAttachment alloc]init];
  385. nText.image = image;
  386. nText.bounds = CGRectMake(0, -3.5, image.size.width * 18 / image.size.height, 18);
  387. NSAttributedString *nImageAttr = [NSAttributedString attributedStringWithAttachment:nText];
  388. NSMutableAttributedString *nAttr = [[NSMutableAttributedString alloc]initWithAttributedString:self.msgLabel.attributedText];
  389. [nAttr replaceCharactersInRange:NSMakeRange(self.imageCount - 1, 1) withAttributedString:nImageAttr];
  390. self.msgLabel.attributedText = nAttr;
  391. }];
  392. }
  393. }
  394. if (!_guardianView.isHidden) {
  395. NSAttributedString *spaceAttr = [[NSAttributedString alloc]initWithString:@" "];
  396. NSTextAttachment *text = [[NSTextAttachment alloc]init];
  397. text.image = [UIImage imageNamed:@"lr_img_corner_ward"];
  398. text.bounds = CGRectMake(0, -3.5, 50, 18);
  399. NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:text];
  400. [attr insertAttributedString:spaceAttr atIndex:0];
  401. [attr insertAttributedString:imageAttr atIndex:0];
  402. // [_guardianView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.guardianModel.guardian_img]];
  403. [_guardianView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.guardian_img] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  404. if (image) {
  405. text.image = image;
  406. }
  407. }];
  408. }
  409. if (!_rankImgView.isHidden) {
  410. NSAttributedString *spaceAttr1 = [[NSAttributedString alloc]initWithString:@" "];
  411. NSAttributedString *spaceAttr = [[NSAttributedString alloc]initWithString:@" "];
  412. NSTextAttachment *text = [[NSTextAttachment alloc]init];
  413. text.image = [UIImage imageNamed:[NSString stringWithFormat:@"level%d",customMessageModel.sender.user_level]];
  414. text.bounds = CGRectMake(0, -3.5, 38, 18);
  415. NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:text];
  416. [attr insertAttributedString:spaceAttr atIndex:0];
  417. [attr insertAttributedString:imageAttr atIndex:0];
  418. [attr insertAttributedString:spaceAttr1 atIndex:0];
  419. }
  420. if (customMessageModel.icon.length) {
  421. UIImage *iconImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:customMessageModel.icon];
  422. NSAttributedString *spaceAttr = [[NSAttributedString alloc]initWithString:@" "];
  423. NSTextAttachment *text = [[NSTextAttachment alloc]init];
  424. if (iconImage) {
  425. text.image = iconImage;
  426. text.bounds = CGRectMake(0, -3.5, 18, 18);
  427. NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:text];
  428. [attr appendAttributedString:spaceAttr];
  429. [attr appendAttributedString:imageAttr];
  430. }else{
  431. [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:customMessageModel.icon] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
  432. NSTextAttachment *nText = [[NSTextAttachment alloc]init];
  433. nText.image = image;
  434. nText.bounds = CGRectMake(0, -3.5, 18, 18);
  435. NSAttributedString *nImageAttr = [NSAttributedString attributedStringWithAttachment:nText];
  436. NSMutableAttributedString *nAttr = [[NSMutableAttributedString alloc]initWithAttributedString:self.msgLabel.attributedText];
  437. [nAttr appendAttributedString:nImageAttr];
  438. self.msgLabel.attributedText = nAttr;
  439. }];
  440. }
  441. }
  442. if (customMessageModel.imageName.length) {
  443. NSAttributedString *spaceAttr = [[NSAttributedString alloc]initWithString:@" "];
  444. NSTextAttachment *text = [[NSTextAttachment alloc]init];
  445. text.image = [UIImage imageNamed:customMessageModel.imageName];
  446. text.bounds = CGRectMake(0, -3.5, 18, 18);
  447. NSAttributedString *imageAttr = [NSAttributedString attributedStringWithAttachment:text];
  448. [attr appendAttributedString:spaceAttr];
  449. [attr appendAttributedString:imageAttr];
  450. }
  451. __weak __typeof(self) ws = self;
  452. if (messageStr.length)
  453. {
  454. NSShadow *shadow = [[NSShadow alloc] init];
  455. shadow.shadowOffset = CGSizeMake(3, 3);
  456. shadow.shadowColor = [UIColor blackColor];
  457. shadow.shadowBlurRadius = 5;
  458. [attr addAttributes:@{NSFontAttributeName : ws.myFont, NSForegroundColorAttributeName : messageLabelColor, NSVerticalGlyphFormAttributeName : @(0)} range:NSMakeRange(self.imageCount, messageStr.length)];
  459. if ([nameStr isEqualToString:ASLocalizedString(@"直播消息:")]) {
  460. [attr addAttributes:@{NSFontAttributeName : ws.myFont, NSForegroundColorAttributeName : myTextColorCommonMessageUser} range:NSMakeRange(self.imageCount, nameStr.length)]; //设置直播消息的字体颜色
  461. }else{
  462. }
  463. //设置用户名称的点击
  464. NSDictionary *attributes = @{NSFontAttributeName : _myFont,
  465. NSForegroundColorAttributeName : myTextColorUser,
  466. NSBackgroundColorAttributeName : kClearColor,
  467. NSStrokeColorAttributeName : myTextColorUser,
  468. };
  469. if (type == MSG_TEXT || type == MSG_SEND_GIFT_SUCCESS || type == MSG_LIGHT || type == MSG_RED_PACKET)
  470. {
  471. [attr addAttributes:attributes range:NSMakeRange(self.imageCount, nameStr.length)];
  472. }
  473. [self.msgLabel setAttributedText:attr];
  474. // [self.msgLabel addLinkWithTextCheckingResult:[NSTextCheckingResult spellCheckingResultWithRange:NSMakeRange(self.imageCount, nameStr.length)] attributes:@{NSForegroundColorAttributeName:myTextColorUser}];
  475. // [_msgLabel setText:attr afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
  476. //
  477. // // http://blog.csdn.net/ys410900285/article/details/25976179
  478. // return mutableAttributedString;
  479. // }];
  480. }
  481. else
  482. {
  483. NSLog(@"==========消息设置出错了");
  484. }
  485. //设置红包的点击
  486. NSDictionary *redPackageAttributes = @{NSFontAttributeName : _myFont,
  487. NSForegroundColorAttributeName : messageLabelColor,
  488. NSBackgroundColorAttributeName : kClearColor,
  489. NSStrokeColorAttributeName : messageLabelColor,
  490. };
  491. if (type == MSG_RED_PACKET)
  492. {
  493. _type = @"0";
  494. // [_msgLabel addLinkWithTextCheckingResult:[NSTextCheckingResult spellCheckingResultWithRange:[messageStr rangeOfString:contentStr]] attributes:redPackageAttributes];
  495. }
  496. //设置用户名称的点击
  497. NSDictionary *attributes = @{NSFontAttributeName : _myFont,
  498. NSForegroundColorAttributeName : myTextColorUser,
  499. NSBackgroundColorAttributeName : kClearColor,
  500. NSStrokeColorAttributeName : myTextColorUser,
  501. };
  502. if (type == MSG_TEXT || type == MSG_SEND_GIFT_SUCCESS || type == MSG_LIGHT || type == MSG_RED_PACKET)
  503. {
  504. _type = @"0";
  505. // [_msgLabel addLinkWithTextCheckingResult:[NSTextCheckingResult spellCheckingResultWithRange:[messageStr rangeOfString:nameStr]] attributes:attributes];
  506. }
  507. if (type == MSG_PAI_SUCCESS || type == MSG_PAI_PAY_TIP || type == MSG_PAI_FAULT || type == MSG_ADD_PRICE || type == MSG_PAY_SUCCESS)
  508. {
  509. _type = @"2";
  510. // [_msgLabel addLinkWithTextCheckingResult:[NSTextCheckingResult spellCheckingResultWithRange:[messageStr rangeOfString:nameStr]] attributes:attributes];
  511. }
  512. _msgLabel.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
  513. _msgLabel.shadowOffset = CGSizeMake(1, 1);
  514. }
  515. - (void)layoutSubviews
  516. {
  517. [super layoutSubviews];
  518. [self relayoutFrameOfSubViews];
  519. }
  520. - (void)relayoutFrameOfSubViews
  521. {
  522. CALayer *gl = _msgBack.layer.sublayers[0];
  523. for (CALayer *sublayer in _msgBack.layer.sublayers) {
  524. if ([gl isKindOfClass:[CAGradientLayer class]]) {
  525. [gl removeFromSuperlayer];
  526. break;
  527. }
  528. }
  529. CGRect frame = self.contentView.frame;
  530. frame.size.width = COMMENT_TABLEVIEW_HEIGHT;
  531. CGSize size = _customMessageModel.avimMsgShowSize;
  532. CGRect rect = frame;
  533. rect.size.height = size.height;
  534. rect.size.width = size.width;
  535. _msgBack.frame = rect;
  536. _msgBack.frame = CGRectMake(CGRectGetMinX(_msgBack.frame), (self.frame.size.height-CGRectGetHeight(_msgBack.frame))/2, CGRectGetWidth(_msgBack.frame), CGRectGetHeight(_msgBack.frame));
  537. rect = _msgBack.bounds;
  538. if (rect.size.height)
  539. {
  540. _msgLabel.frame = CGRectMake(_msgBack.left + 8, _msgBack.top + 10, _msgBack.width - 5, _msgBack.height );
  541. _msgLabel.backgroundColor = kClearColor;
  542. _msgLabel.top = 5;
  543. _msgBack.frame = CGRectMake(_msgLabel.left - 5, _msgLabel.top - 5, _msgLabel.width + 5, _msgLabel.height + 10);
  544. _msgBgImgView.frame = _msgBack.frame;
  545. // _msgBack.backgroundColor = kClearColor;
  546. _msgBgImgView.backgroundColor = kClearColor;
  547. }
  548. CGFloat rankImgViewY = 0;
  549. if ((_customMessageModel && _customMessageModel.icon) || (_customMessageModel && _customMessageModel.imageName))
  550. {
  551. if ([_customMessageModel.icon length] || [_customMessageModel.imageName length])
  552. {
  553. rankImgViewY = 8;
  554. }
  555. else
  556. {
  557. rankImgViewY = 4;
  558. }
  559. }
  560. else
  561. {
  562. rankImgViewY = 4;
  563. }
  564. CGSize messageLabelSize = _customMessageModel.avimMsgShowSize;
  565. if (messageLabelSize.height >= 26 && messageLabelSize.height < 30)
  566. {
  567. rankImgViewY = 8;
  568. }else if (messageLabelSize.height >= 30)
  569. {
  570. rankImgViewY = 4;
  571. }
  572. rankImgViewY = 6;
  573. //_rankImgView的Y值
  574. if (_rankImgView.isHidden == NO)
  575. {
  576. _stealthView.hidden = YES;
  577. CGFloat rankImgViewY = 4;
  578. _msgLabel.left = rect.origin.x;
  579. if ((_customMessageModel && _customMessageModel.icon) || (_customMessageModel && _customMessageModel.imageName))
  580. {
  581. if ([_customMessageModel.icon length] || [_customMessageModel.imageName length])
  582. {
  583. rankImgViewY = 8;
  584. }
  585. else
  586. {
  587. rankImgViewY = 4;
  588. }
  589. }
  590. else
  591. {
  592. rankImgViewY = 6;
  593. }
  594. CGSize messageLabelSize = _customMessageModel.avimMsgShowSize;
  595. if (messageLabelSize.height >= 26 && messageLabelSize.height < 30)
  596. {
  597. rankImgViewY = 8;
  598. }else if (messageLabelSize.height >= 30)
  599. {
  600. rankImgViewY = 6;
  601. }else{
  602. rankImgViewY = 6;
  603. }
  604. _rankImgView.frame = CGRectMake(15, rankImgViewY, 38, 18);
  605. CGFloat rankRight = _rankImgView.right + 2.5;
  606. if (!_vipView.hidden) {
  607. _vipView.frame = CGRectMake(rankRight, rankImgViewY, 50, 18);
  608. rankRight =_vipView.right + 2.5;
  609. }
  610. if (!_guardianView.hidden) {
  611. _guardianView.frame = CGRectMake(rankRight, rankImgViewY, 50, 18);
  612. rankRight =_guardianView.right + 2.5;
  613. }
  614. if (!_nobleVipView.hidden) {
  615. _nobleVipView.frame = CGRectMake(rankRight, rankImgViewY, 18, 18);
  616. rankRight =_nobleVipView.right + 2.5;
  617. }
  618. CALayer *gl = _msgBack.layer.sublayers[0];
  619. if ([gl isKindOfClass:[CAGradientLayer class]]) {
  620. [gl removeFromSuperlayer];
  621. }
  622. //贵族背景颜色
  623. if (_guardianView.hidden && !_nobleVipView.hidden && _stealthView.hidden && _customMessageModel.type != MSG_VIEWER_JOIN && _customMessageModel.type != MSG_LIVING_MESSAGE) {
  624. CAGradientLayer *gl = [CAGradientLayer layer];
  625. gl.frame = _msgBack.bounds;
  626. gl.startPoint = CGPointMake(0, 0.5);
  627. gl.endPoint = CGPointMake(1, 0.5);
  628. gl.colors = @[(__bridge id)[[UIColor colorWithHexString:@"#BC7B02"] colorWithAlphaComponent:0.6].CGColor,(__bridge id)[[UIColor colorWithHexString:@"#E9BD15"] colorWithAlphaComponent:0.6].CGColor];
  629. gl.locations = @[@(0.0),@(1.0f)];
  630. [_msgBack.layer insertSublayer:gl atIndex:0];
  631. }
  632. if (!_guardianView.hidden&& _customMessageModel.type != MSG_VIEWER_JOIN && _customMessageModel.type != MSG_LIVING_MESSAGE) {
  633. CAGradientLayer *gl = [CAGradientLayer layer];
  634. gl.frame = _msgBack.bounds;
  635. gl.startPoint = CGPointMake(0, 0.5);
  636. gl.endPoint = CGPointMake(1, 0.5);
  637. gl.colors = @[(__bridge id)[[UIColor colorWithHexString:@"#9E64FF"] colorWithAlphaComponent:0.6].CGColor,(__bridge id)[[UIColor colorWithHexString:@"#EF60F6"] colorWithAlphaComponent:0.5].CGColor];
  638. gl.locations = @[@(0.0),@(1.0f)];
  639. [_msgBack.layer insertSublayer:gl atIndex:0];
  640. // _msgBack.backgroundColor = [UIColor colorWithHexString:@"#9B00E0"];
  641. }
  642. }else{
  643. CALayer *gl = _msgBack.layer.sublayers[0];
  644. if ([gl isKindOfClass:[CAGradientLayer class]]) {
  645. [gl removeFromSuperlayer];
  646. }
  647. if (messageLabelSize.height >= 25 && messageLabelSize.height < 30)
  648. {
  649. rankImgViewY = 8;
  650. }else if (messageLabelSize.height >= 30)
  651. {
  652. rankImgViewY = 6;
  653. }else{
  654. rankImgViewY = 8;
  655. }
  656. _stealthView.frame = CGRectMake(5, rankImgViewY, 48, 16);
  657. _msgLabel.left = _msgBack.left;
  658. }
  659. }
  660. - (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTextCheckingResult:(NSTextCheckingResult *)result
  661. {
  662. if ([_type isEqualToString:@"2"])
  663. {
  664. if (_delegate && [_delegate respondsToSelector:@selector(clickCellUserInfo:)]) {
  665. [_delegate clickCellUserInfo:self];
  666. }
  667. }else
  668. {
  669. if (result.range.location == 7 || result.range.length == self.customMessageModel.sender.nick_name.length)
  670. {
  671. if (_delegate && [_delegate respondsToSelector:@selector(clickCellNameRange:)])
  672. {
  673. [_delegate clickCellNameRange:self];
  674. }
  675. }
  676. else
  677. {
  678. if (_delegate && [_delegate respondsToSelector:@selector(clickCellMessageRange:)])
  679. {
  680. [_delegate clickCellMessageRange:self];
  681. }
  682. }
  683. }
  684. }
  685. @end
  686. //============================================================================================================================================================================
  687. #pragma mark - TCShowLiveMessageView
  688. @interface TCShowLiveMessageView ()
  689. {
  690. BOOL _isScrolling;
  691. }
  692. @end
  693. @implementation TCShowLiveMessageView
  694. - (instancetype)initWithFrame:(CGRect)frame
  695. {
  696. if (self = [super initWithFrame:frame])
  697. {
  698. self.clipsToBounds = YES;
  699. _canScrollToBottom = YES;
  700. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, COMMENT_TABLEVIEW_WIDTH, COMMENT_TABLEVIEW_HEIGHT)];
  701. _tableView.delegate = self;
  702. _tableView.backgroundColor = kClearColor;
  703. _tableView.dataSource = self;
  704. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  705. _tableView.showsVerticalScrollIndicator = NO;
  706. [self addSubview:_tableView];
  707. _liveMessages = [[NSMutableArray alloc] init];
  708. // 观众首次进入直播室显示的消息
  709. for (CustomMessageModel *customMessageModel in [GlobalVariables sharedInstance].listMsgMArray)
  710. {
  711. [self insertMsg:customMessageModel];
  712. }
  713. }
  714. return self;
  715. }
  716. #pragma mark - ----------------------- 插入消息 -----------------------
  717. #pragma mark 直接显示
  718. - (void)insertMsg:(id<AVIMMsgAble>)item
  719. {
  720. if (item && [item isKindOfClass:[CustomMessageModel class]])
  721. {
  722. @synchronized(_liveMessages)
  723. {
  724. CustomMessageModel *customMessageModel = (CustomMessageModel *)item;
  725. if (!customMessageModel.avimMsgShowSize.height)
  726. {
  727. [customMessageModel prepareForRender];
  728. }
  729. _msgCount++;
  730. [_tableView beginUpdates];
  731. if (_liveMessages.count >= kMaxMsgCount)
  732. {
  733. NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
  734. [_tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationTop];
  735. [_liveMessages removeObjectAtIndex:0];
  736. }
  737. NSIndexPath *index = [NSIndexPath indexPathForRow:_liveMessages.count inSection:0];
  738. [_tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationBottom];
  739. [_liveMessages addObject:item];
  740. [_tableView endUpdates];
  741. if (_canScrollToBottom)
  742. {
  743. [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_liveMessages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
  744. }
  745. }
  746. }
  747. }
  748. #pragma mark 延迟显示
  749. - (void)insertCachedMsg:(AVIMCache *)msgCache
  750. {
  751. NSInteger msgCacheCount = [msgCache count];
  752. if (msgCacheCount == 0) {
  753. return;
  754. }
  755. @synchronized(_liveMessages)
  756. {
  757. _msgCount += msgCacheCount;
  758. while (msgCache.count > 0)
  759. {
  760. CustomMessageModel *customMessageModel = [msgCache deCache];
  761. if (!customMessageModel.avimMsgShowSize.height)
  762. {
  763. [customMessageModel prepareForRender];
  764. }
  765. if (customMessageModel && customMessageModel.type != UPDATE_PK_TICKET)
  766. {
  767. //当收到更新PK收益时不添加消息
  768. [_liveMessages addObject:customMessageModel];
  769. if (_liveMessages.count > kMaxMsgCount)
  770. {
  771. [_liveMessages removeObjectAtIndex:0];
  772. }
  773. }
  774. }
  775. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(reloadMyTableView) object:nil];
  776. [self reloadMyTableView];
  777. if (_canScrollToBottom)
  778. {
  779. [_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_liveMessages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
  780. }
  781. }
  782. }
  783. - (void)reloadMyTableView
  784. {
  785. // NSLog(@"==============load count:%d",++_testIndex);
  786. [_tableView reloadData];
  787. }
  788. #pragma mark - ----------------------- tableView的相关操作 -----------------------
  789. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  790. {
  791. return _liveMessages.count;
  792. }
  793. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  794. {
  795. CustomMessageModel *customMessageModel = [_liveMessages objectAtIndex:indexPath.row];
  796. return customMessageModel.avimMsgShowSize.height + LIVE_MESSAGEVIEW_Margin * 2 + 5;
  797. }
  798. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  799. {
  800. TCShowLiveMsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TCShowLiveMsgTableViewCell"];
  801. if (!cell)
  802. {
  803. cell = [[TCShowLiveMsgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TCShowLiveMsgTableViewCell"];
  804. cell.delegate = self;
  805. }
  806. CustomMessageModel *customMessageModel = [_liveMessages objectAtIndex:indexPath.row];
  807. [cell config:customMessageModel block:^{
  808. [self performSelector:@selector(reloadMyTableView) withObject:nil afterDelay:1];
  809. }];
  810. return cell;
  811. }
  812. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  813. {
  814. // CGPoint translation = [scrollView.panGestureRecognizer translationInView:scrollView.superview];
  815. _canScrollToBottom = NO;
  816. if ([_contentOffsetTimer isValid])
  817. {
  818. [_contentOffsetTimer invalidate];
  819. _contentOffsetTimer = nil;
  820. }
  821. _contentOffsetTimer = [NSTimer scheduledTimerWithTimeInterval:kLiveMessageContentOffsetTime target:self selector:@selector(changeScrollToBottomState) userInfo:nil repeats:NO];
  822. }
  823. - (void)changeScrollToBottomState
  824. {
  825. _canScrollToBottom = !_canScrollToBottom;
  826. }
  827. #pragma mark 点击消息列表中的用户名称
  828. - (void)clickCellNameRange:(TCShowLiveMsgTableViewCell *) cell
  829. {
  830. if (_delegate && [_delegate respondsToSelector:@selector(clickNameRange:)])
  831. {
  832. [_delegate clickNameRange:cell.customMessageModel];
  833. }
  834. }
  835. #pragma mark 点击消息列表中的具体消息内容(目前会响应点击事件的是:红包)
  836. - (void)clickCellMessageRange:(TCShowLiveMsgTableViewCell *) cell
  837. {
  838. if (_delegate && [_delegate respondsToSelector:@selector(clickMessageRange:)])
  839. {
  840. [_delegate clickMessageRange:cell.customMessageModel];
  841. }
  842. }
  843. - (void)clickCellUserInfo:(TCShowLiveMsgTableViewCell *)cell
  844. {
  845. if (_delegate && [_delegate respondsToSelector:@selector(clickUserInfo:)])
  846. {
  847. [_delegate clickUserInfo:cell.customMessageModel.user];
  848. }
  849. }
  850. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  851. {
  852. CustomMessageModel *customMessageModel = [_liveMessages objectAtIndex:indexPath.row];
  853. if (customMessageModel.type== MSG_STARGOODS_SUCCESS)
  854. {
  855. if (_delegate && [_delegate respondsToSelector:@selector(clickGoodsMessage:)])
  856. {
  857. [_delegate clickGoodsMessage:customMessageModel];
  858. }
  859. }else{
  860. if (customMessageModel.type != MSG_VIEWER_JOIN && customMessageModel.type != MSG_LIVING_MESSAGE) {
  861. if (_delegate && [_delegate respondsToSelector:@selector(clickNameRange:)])
  862. {
  863. [_delegate clickNameRange:customMessageModel];
  864. }
  865. }
  866. }
  867. }
  868. - (void)dealloc
  869. {
  870. if ([_contentOffsetTimer isValid])
  871. {
  872. [_contentOffsetTimer invalidate];
  873. _contentOffsetTimer = nil;
  874. }
  875. }
  876. @end