YOUPAIHRSearchView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // YOUPAIHRSearchView.m
  3. // VQU
  4. //
  5. // Created by xiaohaoran on 2021/11/9.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAIHRSearchView.h"
  9. @interface YOUPAIHRSearchView ()<UITableViewDelegate,UITableViewDataSource>
  10. @property (nonatomic, strong) NSMutableArray *youpaiphistoryArray;
  11. @property (nonatomic, strong) UIView *youpaipsearchHistoryView;
  12. @end
  13. @implementation YOUPAIHRSearchView
  14. - (instancetype)initWithFrame:(CGRect)frame youpaiphistoryArray:(NSMutableArray *)historyArr
  15. {
  16. if (self = [super initWithFrame:frame]) {
  17. self.youpaiphistoryArray = historyArr;
  18. [self youpaifsetSubView];
  19. }
  20. return self;
  21. }
  22. -(void)youpaifsetSubView{
  23. [self addSubview:self.youpaipsearchHistoryView];
  24. }
  25. - (UIView *)youpaipsearchHistoryView
  26. {
  27. if (!_youpaipsearchHistoryView) {
  28. if (_youpaiphistoryArray.count > 0) {
  29. self.youpaipsearchHistoryView = [self youpaifsetViewWithOriginY:0 title:@"历史记录" textArr:self.youpaiphistoryArray];
  30. } else {
  31. self.youpaipsearchHistoryView = [self youpaifsetNoHistoryView];
  32. }
  33. }
  34. return _youpaipsearchHistoryView;
  35. }
  36. - (UIView *)youpaifsetViewWithOriginY:(CGFloat)riginY title:(NSString *)title textArr:(NSMutableArray *)textArr
  37. {
  38. UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 20, 14, 14)];
  39. imageView.image = [UIImage imageNamed:@"vqu_images_chatroom_search_his"];
  40. UIView *view = [[UIView alloc] init];
  41. UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+6, 10, KScreenWidth - 30 - 45, 30)];
  42. titleL.text = title;
  43. titleL.font = [UIFont systemFontOfSize:15];
  44. titleL.textColor = [UIColor whiteColor];
  45. titleL.textAlignment = NSTextAlignmentLeft;
  46. [view addSubview:titleL];
  47. [view addSubview:imageView];
  48. if ([title isEqualToString:@"历史记录"]) {
  49. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  50. btn.frame = CGRectMake(KScreenWidth - 45, 10, 28, 30);
  51. [btn setImage:[UIImage imageNamed:@"vqu_images_chatroom_sort_recycle"] forState:UIControlStateNormal];
  52. [btn addTarget:self action:@selector(youpaifclearnSearchHistory:) forControlEvents:UIControlEventTouchUpInside];
  53. [view addSubview:btn];
  54. }
  55. CGFloat y = 10 + 40;
  56. CGFloat letfWidth = 15;
  57. for (int i = 0; i < textArr.count; i++) {
  58. NSString *text = textArr[i];
  59. CGFloat width = [self youpaifgetWidthWithStr:text] + 30;
  60. if (letfWidth + width + 15 > KScreenWidth) {
  61. if (y >= 130 && [title isEqualToString:@"历史记录"]) {
  62. [self youpaifremoveTestDataWithTextArr:textArr index:i];
  63. break;
  64. }
  65. y += 40;
  66. letfWidth = 15;
  67. }
  68. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(letfWidth, y, width, 30)];
  69. label.userInteractionEnabled = YES;
  70. label.font = [UIFont systemFontOfSize:12];
  71. label.text = text;
  72. label.textColor = [UIColor whiteColor];
  73. label.layer.cornerRadius = 12.5;
  74. label.layer.masksToBounds = YES;
  75. label.textAlignment = NSTextAlignmentCenter;
  76. label.backgroundColor = HexColorFromRGB(0x4F4B5B);
  77. [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaiftagDidCLick:)]];
  78. [view addSubview:label];
  79. letfWidth += width + 10;
  80. }
  81. view.frame = CGRectMake(0, riginY, KScreenWidth, y + 40);
  82. return view;
  83. }
  84. - (void)youpaiftagDidCLick:(UITapGestureRecognizer *)tap
  85. {
  86. UILabel *label = (UILabel *)tap.view;
  87. if (self.youpaiptapAction) {
  88. self.youpaiptapAction(label.text);
  89. }
  90. }
  91. - (void)youpaifclearnSearchHistory:(UIButton *)sender
  92. {
  93. [self.youpaipsearchHistoryView removeFromSuperview];
  94. self.youpaipsearchHistoryView = [self youpaifsetNoHistoryView];
  95. [_youpaiphistoryArray removeAllObjects];
  96. [NSKeyedArchiver archiveRootObject:_youpaiphistoryArray toFile:KHistorySearchPath];
  97. [self addSubview:self.youpaipsearchHistoryView];
  98. }
  99. - (UIView *)youpaifsetNoHistoryView
  100. {
  101. UIView *historyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 80)];
  102. UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 20, 14, 14)];
  103. imageView.image = [UIImage imageNamed:@"vqu_images_chatroom_search_his"];
  104. UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+6, 10, KScreenWidth - 30, 30)];
  105. titleL.text = @"历史记录";
  106. titleL.font = [UIFont systemFontOfSize:15];
  107. titleL.textColor = [UIColor whiteColor];
  108. titleL.textAlignment = NSTextAlignmentLeft;
  109. UILabel *youpaipnotextL = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleL.frame) + 10, 100, 20)];
  110. youpaipnotextL.text = @"无搜索历史";
  111. youpaipnotextL.font = [UIFont systemFontOfSize:12];
  112. youpaipnotextL.textColor = [UIColor whiteColor];
  113. youpaipnotextL.textAlignment = NSTextAlignmentLeft;
  114. [historyView addSubview:titleL];
  115. [historyView addSubview:imageView];
  116. [historyView addSubview:youpaipnotextL];
  117. return historyView;
  118. }
  119. - (CGFloat)youpaifgetWidthWithStr:(NSString *)text
  120. {
  121. CGFloat width = [text boundingRectWithSize:CGSizeMake(KScreenWidth, 40) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]} context:nil].size.width;
  122. return width;
  123. }
  124. - (void)youpaifremoveTestDataWithTextArr:(NSMutableArray *)testArr index:(int)index
  125. {
  126. NSRange range = {index, testArr.count - index - 1};
  127. [testArr removeObjectsInRange:range];
  128. [NSKeyedArchiver archiveRootObject:testArr toFile:KHistorySearchPath];
  129. }
  130. @end