commDetailCell.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // commDetailCell.m
  3. // yunbaolive
  4. //
  5. // Created by Boom on 2018/12/17.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "commDetailCell.h"
  9. #import "YBCenterVC.h"
  10. @implementation commDetailCell
  11. - (void)awakeFromNib {
  12. [super awakeFromNib];
  13. // Initialization code
  14. self.contentView.backgroundColor = [UIColor clearColor];
  15. }
  16. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  17. [super setSelected:selected animated:animated];
  18. // Configure the view for the selected state
  19. }
  20. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  21. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  22. if (self) {
  23. _iconIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 2, 22, 22)];
  24. _iconIV.layer.masksToBounds = YES;
  25. _iconIV.layer.cornerRadius = 12;
  26. [self.contentView addSubview:_iconIV];
  27. //_nameL = [[UILabel alloc]initWithFrame:CGRectMake(_iconIV.right+2, 3, self.contentView.width, 20)];
  28. _nameL = [[UILabel alloc]init];
  29. _nameL.textColor = RGB_COLOR(@"#969696", 1);
  30. _nameL.font = [UIFont systemFontOfSize:14];
  31. [self.contentView addSubview:_nameL];
  32. [_nameL mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.centerY.equalTo(_iconIV);
  34. make.left.equalTo(_iconIV.mas_right).offset(2);
  35. }];
  36. //_authorL = [[UILabel alloc]initWithFrame:CGRectMake(_nameL.right+5, 5, 24, 14)];
  37. _authorL = [[UILabel alloc]init];
  38. _authorL.textColor = [UIColor whiteColor];
  39. _authorL.font = [UIFont systemFontOfSize:10];
  40. _authorL.backgroundColor = RGB_COLOR(@"#ff0066", 1);//Pink_Cor;
  41. _authorL.textAlignment = NSTextAlignmentCenter;
  42. _authorL.layer.masksToBounds = YES;
  43. _authorL.layer.cornerRadius = 3;
  44. _authorL.text = YZMsg(@"作者");
  45. [self.contentView addSubview:_authorL];
  46. [_authorL mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.centerY.equalTo(_iconIV);
  48. make.left.equalTo(_nameL.mas_right).offset(5);
  49. make.width.equalTo(@24);
  50. make.height.equalTo(@14);
  51. }];
  52. _zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  53. _zanBtn.frame = CGRectMake(_window_width-45-15-30, 0, 30, 30);
  54. _zanBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
  55. [_zanBtn addTarget:self action:@selector(clickReplayZanBtn) forControlEvents:UIControlEventTouchUpInside];
  56. [self.contentView addSubview:_zanBtn];
  57. _zanL = [[UILabel alloc]initWithFrame:CGRectMake(_zanBtn.left-10, _zanBtn.bottom, 50, 15)];
  58. _zanL.textAlignment = NSTextAlignmentCenter;
  59. _zanL.font = SYS_Font(11);
  60. [self.contentView addSubview:_zanL];
  61. _contentL = [[YYLabel alloc]init];
  62. _contentL.textColor =UIColor.blackColor;
  63. _contentL.font = [UIFont systemFontOfSize:14];
  64. _contentL.numberOfLines = 0;
  65. self.contentView.backgroundColor = UIColor.whiteColor;
  66. [self.contentView addSubview:_contentL];
  67. _voiceView = [[UIView alloc]init];
  68. _voiceView.hidden = YES;
  69. [self.contentView addSubview:_voiceView];
  70. [_voiceView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.left.equalTo(_nameL.mas_left);
  72. make.top.equalTo(_nameL.mas_bottom).offset(2);
  73. make.width.mas_equalTo(70);
  74. make.height.mas_equalTo(20);
  75. }];
  76. UIButton *playBtn = [UIButton buttonWithType:0];
  77. [playBtn setImage:[UIImage imageNamed:@"commen-yuyin"] forState:0];
  78. [playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  79. [_voiceView addSubview:playBtn];
  80. [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.center.width.height.equalTo(_voiceView);
  82. }];
  83. _voiceNorImgV = [[UIImageView alloc]init];
  84. _voiceNorImgV.image = [UIImage imageNamed:@"comment_voice_nor"];
  85. [_voiceView addSubview:_voiceNorImgV];
  86. [_voiceNorImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.centerY.equalTo(_voiceView);
  88. make.left.equalTo(_voiceView).offset(8);
  89. make.width.height.mas_equalTo(7);
  90. }];
  91. _voiceTimeL = [[UILabel alloc]init];
  92. _voiceTimeL.textColor = [UIColor whiteColor];
  93. _voiceTimeL.font = [UIFont systemFontOfSize:8];
  94. [_voiceView addSubview:_voiceTimeL];
  95. [_voiceTimeL mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.centerY.equalTo(_voiceView);
  97. make.right.equalTo(_voiceView).offset(-4);
  98. }];
  99. _animationView = [[YYAnimatedImageView alloc]init];
  100. NSURL *url = [[NSBundle mainBundle] URLForResource:@"voice_pinglun" withExtension:@"gif"];
  101. _animationView.yy_imageURL = url;
  102. _animationView.hidden = YES;
  103. [_voiceView addSubview:_animationView];
  104. [_animationView mas_makeConstraints:^(MASConstraintMaker *make) {
  105. make.center.equalTo(_voiceView);
  106. make.width.mas_equalTo(13);
  107. make.height.mas_equalTo(10);
  108. }];
  109. _longPressView = [[UIView alloc]init];
  110. [self.contentView addSubview:_longPressView];
  111. [self.contentView sendSubviewToBack:_longPressView];
  112. [_longPressView mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.top.equalTo(_iconIV.mas_top).offset(-5);
  114. make.left.equalTo(_iconIV.mas_left).offset(-5);
  115. make.right.equalTo(_zanBtn.mas_right).offset(5);
  116. make.bottom.equalTo(_contentL.mas_bottom).offset(3);
  117. }];
  118. UIButton *iconShadowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  119. [iconShadowBtn addTarget:self action:@selector(clickIconBtn) forControlEvents:UIControlEventTouchUpInside];
  120. [self.contentView addSubview:iconShadowBtn];
  121. [iconShadowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.centerX.centerY.equalTo(_iconIV);
  123. make.width.equalTo(_iconIV.mas_width).offset(10);
  124. make.height.equalTo(_iconIV.mas_height).offset(10);
  125. }];
  126. }
  127. return self;
  128. }
  129. - (void)playBtnClick:(UIButton *)sender{
  130. [self.delegate playVoice:_model andCell:self];
  131. }
  132. -(void)setAuthorID:(NSString *)authorID {
  133. _authorID = authorID;
  134. }
  135. - (void)setModel:(detailmodel *)model{
  136. _model = model;
  137. _authorL.hidden = YES;
  138. if ([_authorID isEqual:_model.ID]) {
  139. _authorL.hidden = NO;
  140. }
  141. if ([model.isvoice isEqual:@"1"]) {
  142. _voiceView.hidden = NO;
  143. _voiceView.height = NO;
  144. _animationView.hidden = YES;
  145. _voiceTimeL.text = [NSString stringWithFormat:@"%@\"",model.voiceTime];
  146. }else{
  147. _voiceView.hidden = YES;
  148. }
  149. [_iconIV sd_setImageWithURL:[NSURL URLWithString:_model.avatar_thumb]];
  150. _nameL.text = _model.user_nickname;
  151. if ([_model.islike isEqual:@"1"]) {
  152. [_zanBtn setImage:[UIImage imageNamed:@"likecomment-click"] forState:0];
  153. _zanL.textColor = Pink_Cor;
  154. }else{
  155. [_zanBtn setImage:[UIImage imageNamed:@"likecomment"] forState:0];
  156. _zanL.textColor = RGB_COLOR(@"#828282", 1);
  157. }
  158. _zanL.text = _model.likes;
  159. _contentL.frame = _model.contentRect;
  160. NSArray *resultArr = [PublicObj machesWithPattern:emojiPattern andStr:_model.content];
  161. if (!resultArr) return;
  162. NSUInteger lengthDetail = 0;
  163. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:_model.content];
  164. //遍历所有的result 取出range
  165. for (NSTextCheckingResult *result in resultArr) {
  166. //取出图片名
  167. NSString *imageName = [_model.content substringWithRange:NSMakeRange(result.range.location, result.range.length)];
  168. if ([lagType isEqual:EN]) {
  169. if ([imageName isEqual:@"[互粉]"] ||
  170. [imageName isEqual:@"[关注]"] ||
  171. [imageName isEqual:@"[粉]"] ||
  172. [imageName isEqual:@"[给力]"]) {
  173. imageName = [imageName stringByAppendingFormat:@"_en"];
  174. }
  175. }
  176. NSLog(@"--------%@",imageName);
  177. NSTextAttachment *attach = [[NSTextAttachment alloc] init];
  178. UIImage *emojiImage = [UIImage imageNamed:imageName];
  179. NSAttributedString *imageString;
  180. if (emojiImage) {
  181. /*
  182. attach.image = emojiImage;
  183. attach.bounds = CGRectMake(0, -2, 15, 15);
  184. imageString = [NSAttributedString attributedStringWithAttachment:attach];
  185. */
  186. UIImageView *imageV = [[UIImageView alloc] init];
  187. imageV.frame = CGRectMake(0, -2, 15, 15);
  188. imageV.image = emojiImage;
  189. imageString = [NSAttributedString yy_attachmentStringWithContent:imageV contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(15, 15) alignToFont:_contentL.font alignment:YYTextVerticalAlignmentCenter];
  190. }else{
  191. imageString = [[NSMutableAttributedString alloc]initWithString:imageName];
  192. }
  193. //图片附件的文本长度是1
  194. NSLog(@"emoj===%zd===size-w:%f==size-h:%f",imageString.length,imageString.size.width,imageString.size.height);
  195. NSUInteger length = attStr.length;
  196. NSRange newRange = NSMakeRange(result.range.location - lengthDetail, result.range.length);
  197. [attStr replaceCharactersInRange:newRange withAttributedString:imageString];
  198. lengthDetail += length - attStr.length;
  199. }
  200. NSAttributedString *dateStr = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@" %@",_model.datetime] attributes:@{NSForegroundColorAttributeName:RGB_COLOR(@"#969696", 1),NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  201. [attStr appendAttributedString:dateStr];
  202. //不同字体居中对齐、基线偏移比率
  203. [attStr addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (14 - 11)) range:NSMakeRange(attStr.length - _model.datetime.length, _model.datetime.length)];
  204. [attStr addAttribute:NSForegroundColorAttributeName value:UIColor.blackColor range:attStr.yy_rangeOfAll];//RGB_COLOR(@"#FFFFFF", 1)
  205. //给被@加颜色
  206. NSData *mix_data = [_model.at_info dataUsingEncoding:NSUTF8StringEncoding];
  207. NSArray *atArray = [NSJSONSerialization JSONObjectWithData:mix_data options:NSJSONReadingAllowFragments error:nil];
  208. if (atArray.count>0) {
  209. //有@
  210. for (int i=0; i<atArray.count; i++) {
  211. NSDictionary *u_dic = atArray[i];
  212. NSString *uname = [NSString stringWithFormat:@"@%@",[u_dic valueForKey:@"name"]];
  213. if ([_model.content containsString:uname]) {
  214. NSRange uRang = [_model.content rangeOfString:uname];
  215. // [attStr addAttribute:NSForegroundColorAttributeName value:AtCol range:uRang];
  216. [attStr yy_setTextHighlightRange:uRang color:AtCol backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
  217. NSLog(@"回复进入个人中心");
  218. YBCenterVC *center = [[YBCenterVC alloc]init];
  219. center.otherUid = minstr([u_dic valueForKey:@"uid"]);
  220. center.isPush = YES;
  221. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:center animated:YES];
  222. if (self.delegate && [self.delegate respondsToSelector:@selector(endEnterCenter)]) {
  223. [self.delegate endEnterCenter];
  224. }
  225. }];
  226. }
  227. }
  228. }
  229. if ([_model.touid intValue]>0) {
  230. NSRange repeatRange = [[attStr string]rangeOfString:[NSString stringWithFormat:@"%@:",[_model.touserinfo valueForKey:@"user_nickname"]]];
  231. [attStr addAttributes:@{NSForegroundColorAttributeName:RGB_COLOR(@"#969696", 1)} range:repeatRange];
  232. }
  233. //更新到label上
  234. _contentL.attributedText = attStr;
  235. }
  236. -(void)clickReplayZanBtn {
  237. if ([[Config getOwnID] intValue]<=0 && _delegate && [_delegate respondsToSelector:@selector(warningLogin)]) {
  238. //[PublicObj warnLogin];
  239. [_delegate warningLogin];
  240. return;
  241. }
  242. if ([_model.ID isEqual:[Config getOwnID]]) {
  243. [MBProgressHUD showError:YZMsg(@"不能给自己的评论点赞")];
  244. return;
  245. }
  246. NSString *url = [NSString stringWithFormat:@"Video.addCommentLike&uid=%@&commentid=%@&token=%@",[Config getOwnID],_model.parentid,[Config getOwnToken]];
  247. [YBNetworking postWithUrl:url Dic:nil Suc:^(int code, id info, NSString *msg) {
  248. if (code == 0) {
  249. //动画
  250. dispatch_async(dispatch_get_main_queue(), ^{
  251. [_zanBtn.imageView.layer addAnimation:[PublicObj bigToSmallRecovery] forKey:nil];
  252. });
  253. NSDictionary *infoDic = [info firstObject];
  254. NSString *islike = [NSString stringWithFormat:@"%@",[infoDic valueForKey:@"islike"]];
  255. NSString *likes = [NSString stringWithFormat:@"%@",[infoDic valueForKey:@"likes"]];
  256. _zanL.text = likes;
  257. if ([islike isEqual:@"1"]) {
  258. [_zanBtn setImage:[UIImage imageNamed:@"likecomment-click"] forState:0];
  259. _zanL.textColor = Pink_Cor;
  260. }else{
  261. [_zanBtn setImage:[UIImage imageNamed:@"likecomment"] forState:0];
  262. _zanL.textColor = RGB_COLOR(@"#828282", 1);
  263. }
  264. if ([_delegate respondsToSelector:@selector(refreshDtailDataWithId:andLikes:islike:)]) {
  265. [_delegate refreshDtailDataWithId:_model.parentid andLikes:likes islike:islike];
  266. }
  267. }else{
  268. [MBProgressHUD showError:msg];
  269. }
  270. } Fail:^(id fail) {
  271. }];
  272. }
  273. -(void)clickIconBtn {
  274. if ([[Config getOwnID] intValue]<=0 && _delegate && [_delegate respondsToSelector:@selector(warningLogin)]) {
  275. [_delegate warningLogin];
  276. return;
  277. }
  278. if (self.delegate && [self.delegate respondsToSelector:@selector(endEditOfGoHome)]) {
  279. [self.delegate endEditOfGoHome];
  280. }
  281. [[NSNotificationCenter defaultCenter]postNotificationName:@"goCenterNot" object:@"1"];
  282. YBCenterVC *center = [[YBCenterVC alloc]init];
  283. center.otherUid = _model.ID;
  284. center.isPush = YES;
  285. center.hidesBottomBarWhenPushed = YES;
  286. center.backEvent = ^{
  287. [[NSNotificationCenter defaultCenter]postNotificationName:@"goCenterNot" object:@"0"];
  288. };
  289. [[YBBaseAppDelegate sharedAppDelegate] pushViewController:center animated:YES];
  290. }
  291. @end