JXSearchShowCell.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // JXSearchRecordCell.m
  3. // shiku_im
  4. //
  5. // Created by IMAC on 2019/8/27.
  6. // Copyright © 2019 Reese. All rights reserved.
  7. //
  8. #import "JXSearchShowCell.h"
  9. @implementation JXSearchShowCell
  10. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withNewStyle:(JXSearchShowCellStyle)newStyle{
  11. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  12. if (self) {
  13. self.cellStyle = newStyle;
  14. switch (newStyle) {
  15. case JXSearchShowCellStyleUser:
  16. {
  17. self.headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 9.83333, 40, 40)];
  18. self.headImgView.layer.cornerRadius = 20;
  19. self.headImgView.layer.masksToBounds = YES;
  20. [self.contentView addSubview:self.headImgView];
  21. [self setSeparatorInset:UIEdgeInsetsMake(0, 80, 0, 0)];
  22. self.aboveLable = [[UILabel alloc] initWithFrame:CGRectMake(80 ,20, JX_SCREEN_WIDTH - 90 ,22)];
  23. self.aboveLable.font = [UIFont systemFontOfSize:16];
  24. self.aboveLable.textAlignment = NSTextAlignmentLeft;
  25. self.aboveLable.textColor = [UIColor blackColor];
  26. [self.contentView addSubview:self.aboveLable];
  27. self.belowLable = [[UILabel alloc] initWithFrame:CGRectMake(80 ,28.3333, 0 ,0)];
  28. self.belowLable.font = [UIFont systemFontOfSize:13];
  29. self.belowLable.textColor = [UIColor grayColor];
  30. [self.contentView addSubview:self.belowLable];
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. self.selectView = [[UIView alloc] initWithFrame:CGRectMake(8, 0, JX_SCREEN_WIDTH - 16, 60)];
  33. self.selectView.backgroundColor = [UIColor grayColor];
  34. self.selectView.alpha = 0.3;
  35. self.selectView.hidden = YES;
  36. [self.contentView addSubview:self.selectView];
  37. }
  38. break;
  39. case JXSearchShowCellStyleRecord:
  40. {
  41. self.headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 9.83333, 40, 40)];
  42. self.headImgView.layer.cornerRadius = 20;
  43. self.headImgView.layer.masksToBounds = YES;
  44. [self.contentView addSubview:self.headImgView];
  45. [self setSeparatorInset:UIEdgeInsetsMake(0, 80, 0, 0)];
  46. self.aboveLable = [[UILabel alloc] initWithFrame:CGRectMake(80 ,10, 108.333 ,14.3333)];
  47. self.aboveLable.font = [UIFont systemFontOfSize:13];
  48. self.aboveLable.textAlignment = NSTextAlignmentLeft;
  49. self.aboveLable.textColor = HEXCOLOR(0x999999);
  50. [self.contentView addSubview:self.aboveLable];
  51. self.belowLable = [[UILabel alloc] initWithFrame:CGRectMake(80 ,30, JX_SCREEN_WIDTH - 90 ,18)];
  52. self.belowLable.font = [UIFont systemFontOfSize:15];
  53. self.belowLable.textAlignment = NSTextAlignmentLeft;
  54. self.belowLable.textColor = [UIColor blackColor];
  55. [self.contentView addSubview:self.belowLable];
  56. self.rightLable = [[UILabel alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH - 170 , 10 , 160 , 14.3333)];
  57. self.rightLable.textAlignment = NSTextAlignmentRight;
  58. self.rightLable.font = [UIFont systemFontOfSize:13];
  59. self.rightLable.textColor = [UIColor grayColor];
  60. [self.contentView addSubview:self.rightLable];
  61. }
  62. default:
  63. break;
  64. }
  65. }
  66. return self;
  67. }
  68. - (void)setHeadImg:(NSString *)headImg{
  69. _headImg = headImg;
  70. _headImgView.image = [UIImage imageNamed:_headImg];
  71. }
  72. - (void)setAboveText:(NSString *)aboveText{
  73. _aboveText = aboveText;
  74. _aboveLable.text = _aboveText;
  75. }
  76. - (void)setAboveAttributedText:(NSMutableAttributedString *)aboveAttributedText{
  77. _aboveAttributedText = aboveAttributedText;
  78. _aboveLable.attributedText = _aboveAttributedText;
  79. }
  80. - (void)setBelowText:(NSString *)belowText{
  81. switch (_cellStyle) {
  82. case JXSearchShowCellStyleUser:
  83. {
  84. _belowText = belowText;
  85. _belowLable.text = _belowText;
  86. _aboveLable.frame = CGRectMake(80 ,7, JX_SCREEN_WIDTH - 90 ,22);
  87. _belowLable.frame = CGRectMake(80 ,35.3333, JX_SCREEN_WIDTH - 90 ,17);
  88. }
  89. break;
  90. case JXSearchShowCellStyleRecord:
  91. {
  92. _belowText = belowText;
  93. NSAttributedString *str = [self setContentLabelStr:_belowText searchText:self.searchText];
  94. _belowLable.attributedText = str;
  95. }
  96. default:
  97. break;
  98. }
  99. }
  100. - (void)setBelowAttributedText:(NSMutableAttributedString *)belowAttributedText{
  101. switch (_cellStyle) {
  102. case JXSearchShowCellStyleUser:
  103. {
  104. _belowAttributedText = belowAttributedText;
  105. _belowLable.attributedText = _belowAttributedText;
  106. _aboveLable.frame = CGRectMake(80 ,7, JX_SCREEN_WIDTH - 90 ,22);
  107. _belowLable.frame = CGRectMake(80 ,35.3333, JX_SCREEN_WIDTH - 90 ,17);
  108. }
  109. break;
  110. case JXSearchShowCellStyleRecord:
  111. {
  112. _belowAttributedText = belowAttributedText;
  113. _belowLable.attributedText = _belowAttributedText;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. - (void)setRightText:(NSString *)rightText{
  121. _rightText = rightText;
  122. _rightLable.text = _rightText;
  123. }
  124. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  125. [super setSelected:selected animated:animated];
  126. if (selected) {
  127. switch (self.cellStyle) {
  128. case JXSearchShowCellStyleUser:
  129. self.selectView.hidden = NO;
  130. [self performSelector:@selector(hiddenSelectView) withObject:nil afterDelay:0.5];
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. }
  137. - (void)cutSelectedView{
  138. CAShapeLayer *layer = [[CAShapeLayer alloc] init];
  139. CGMutablePathRef pathRef = CGPathCreateMutable();
  140. CGRect bounds = CGRectInset(self.selectView.bounds, 0, 0);
  141. CGFloat cornerRadius = 7;
  142. CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
  143. CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
  144. CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
  145. CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
  146. layer.path = pathRef;
  147. CFRelease(pathRef);
  148. layer.fillColor = [UIColor grayColor].CGColor;
  149. self.selectView.backgroundColor = [UIColor clearColor];
  150. [self.selectView.layer addSublayer:layer];
  151. self.selectView.alpha = 0.3;
  152. }
  153. - (void)hiddenSelectView{
  154. self.selectView.hidden = YES;
  155. }
  156. - (void)getMessageRange:(NSString*)message :(NSMutableArray*)array {
  157. NSRange range=[message rangeOfString: @"["];
  158. NSRange range1=[message rangeOfString: @"]"];
  159. // 动画过滤
  160. if ([message isEqualToString:[NSString stringWithFormat:@"[%@]",Localized(@"emojiVC_Emoji")]]) {
  161. [array addObject:message];
  162. return;
  163. }
  164. //判断当前字符串是否还有表情的标志。
  165. if (range.length>0 && range1.length>0 && range1.location > range.location) {
  166. if (range.location > 0) {
  167. NSString *str = [message substringToIndex:range.location];
  168. NSString *str1 = [message substringFromIndex:range.location];
  169. [array addObject:str];
  170. [self getMessageRange:str1 :array];
  171. }else {
  172. NSString *emojiString = [message substringWithRange:NSMakeRange(range.location + 1, range1.location - 1)];
  173. BOOL isEmoji = NO;
  174. NSString *str;
  175. NSString *str1;
  176. for (NSMutableDictionary *dic in g_constant.emojiArray) {
  177. NSString *emoji = [dic objectForKey:@"english"];
  178. if ([emoji isEqualToString:emojiString]) {
  179. isEmoji = YES;
  180. break;
  181. }
  182. }
  183. if (isEmoji) {
  184. str = [message substringWithRange:NSMakeRange(range.location, range1.location + 1)];
  185. str1 = [message substringFromIndex:range1.location + 1];
  186. [array addObject:str];
  187. }else{
  188. NSString *posString = [message substringWithRange:NSMakeRange(range.location + 1, range1.location)];
  189. NSRange posRange = [posString rangeOfString:@"["];
  190. if (posRange.location != NSNotFound) {
  191. str = [message substringToIndex:posRange.location + 1];
  192. str1 = [message substringFromIndex:posRange.location + 1];
  193. [array addObject:str];
  194. }else{
  195. str = [message substringToIndex:range.location + 1];
  196. str1 = [message substringFromIndex:range.location + 1];
  197. [array addObject:str];
  198. }
  199. }
  200. [self getMessageRange:str1 :array];
  201. }
  202. }else if (range.length>0 && range1.length>0 && range1.location < range.location){
  203. NSString *str = [message substringToIndex:range1.location + 1];
  204. NSString *str1 = [message substringFromIndex:range1.location + 1];
  205. [array addObject:str];
  206. [self getMessageRange:str1 :array];
  207. }else if (message != nil) {
  208. [array addObject:message];
  209. }
  210. }
  211. - (NSAttributedString *)setContentLabelStr:(NSString *)str searchText:(NSString *)searchText{
  212. NSMutableArray *conArray = [NSMutableArray array];
  213. [self getMessageRange:str :conArray];
  214. NSMutableArray *contentArray = [NSMutableArray array];
  215. _num = 0;
  216. [self splicingString:conArray inArray:contentArray];
  217. NSMutableAttributedString *strM = [[NSMutableAttributedString alloc] init];
  218. NSInteger count = contentArray.count;
  219. if (contentArray.count > 15) {
  220. count = 15;
  221. }
  222. for (NSInteger i = 0; i < count; i ++) {
  223. NSString *object = contentArray[i];
  224. // NSLog(@"%@",object);
  225. BOOL flag = NO;
  226. if ([object hasSuffix:@"]"]&&[object hasPrefix:@"["]) {
  227. //如果是表情用iOS中附件代替string在label上显示
  228. NSTextAttachment *imageStr = [[NSTextAttachment alloc]init];
  229. NSString *imageShortName = [object substringWithRange:NSMakeRange(1, object.length - 2)];
  230. for (NSInteger i = 0; i < g_constant.emojiArray.count; i ++) {
  231. NSDictionary *dict = g_constant.emojiArray[i];
  232. NSString *imageName = dict[@"english"];
  233. if ([imageName isEqualToString:imageShortName]) {
  234. imageStr.image = [UIImage imageNamed:dict[@"filename"]];
  235. flag = YES;
  236. break; }
  237. }
  238. if (!flag) {
  239. [strM appendAttributedString:[[NSAttributedString alloc] initWithString:object]];
  240. NSRange range = [object rangeOfString:Localized(@"JX_Draft")];
  241. [strM addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
  242. range = [object rangeOfString:Localized(@"JX_Someone@Me")];
  243. [strM addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
  244. continue;
  245. }
  246. // imageStr.image = [UIImage imageNamed:[object substringWithRange:NSMakeRange(1, object.length - 2)]];
  247. //这里对图片的大小进行设置一般来说等于文字的高度
  248. CGFloat height = self.belowLable.font.lineHeight + 1;
  249. imageStr.bounds = CGRectMake(0, -4, height, height);
  250. NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:imageStr];
  251. [strM appendAttributedString:attrString];
  252. }else{
  253. //如果不是表情直接进行拼接
  254. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:object];
  255. NSRange attRange = [object rangeOfString:self.searchText];
  256. if (attRange.location != NSNotFound) {
  257. [attString addAttribute:NSForegroundColorAttributeName value:[g_theme themeColor] range:attRange];
  258. }
  259. [strM appendAttributedString:attString];
  260. NSRange range = [object rangeOfString:Localized(@"JX_Draft")];
  261. [strM addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
  262. range = [object rangeOfString:Localized(@"JX_Someone@Me")];
  263. [strM addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
  264. }
  265. }
  266. return strM;
  267. }
  268. //拼接字符串
  269. - (void)splicingString:(NSMutableArray *)array inArray:(NSMutableArray *)contentArray{
  270. if (_num >= array.count) {
  271. return;
  272. }
  273. NSString *str = [NSString string];
  274. for (NSInteger i = _num; i < array.count; i++) {
  275. NSString *object = array[i];
  276. if ([object hasSuffix:@"]"]&&[object hasPrefix:@"["]) {
  277. [contentArray addObject:str];
  278. [contentArray addObject:object];
  279. _num = i + 1;
  280. break;
  281. }else{
  282. _num = i + 1;
  283. str = [str stringByAppendingString:object];
  284. if (_num >= array.count) {
  285. [contentArray addObject:str];
  286. return;
  287. }
  288. }
  289. }
  290. [self splicingString:array inArray:contentArray];
  291. }
  292. @end