123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // YOUPAIHRSearchView.m
- // VQU
- //
- // Created by xiaohaoran on 2021/11/9.
- // Copyright © 2021 MS. All rights reserved.
- //
- #import "YOUPAIHRSearchView.h"
- @interface YOUPAIHRSearchView ()<UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic, strong) NSMutableArray *youpaiphistoryArray;
- @property (nonatomic, strong) UIView *youpaipsearchHistoryView;
- @end
- @implementation YOUPAIHRSearchView
- - (instancetype)initWithFrame:(CGRect)frame youpaiphistoryArray:(NSMutableArray *)historyArr
- {
- if (self = [super initWithFrame:frame]) {
- self.youpaiphistoryArray = historyArr;
-
- [self youpaifsetSubView];
- }
- return self;
- }
- -(void)youpaifsetSubView{
-
- [self addSubview:self.youpaipsearchHistoryView];
-
- }
- - (UIView *)youpaipsearchHistoryView
- {
- if (!_youpaipsearchHistoryView) {
- if (_youpaiphistoryArray.count > 0) {
- self.youpaipsearchHistoryView = [self youpaifsetViewWithOriginY:0 title:@"历史记录" textArr:self.youpaiphistoryArray];
- } else {
- self.youpaipsearchHistoryView = [self youpaifsetNoHistoryView];
- }
- }
- return _youpaipsearchHistoryView;
- }
- - (UIView *)youpaifsetViewWithOriginY:(CGFloat)riginY title:(NSString *)title textArr:(NSMutableArray *)textArr
- {
- UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 20, 14, 14)];
- imageView.image = [UIImage imageNamed:@"vqu_images_chatroom_search_his"];
- UIView *view = [[UIView alloc] init];
- UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+6, 10, KScreenWidth - 30 - 45, 30)];
- titleL.text = title;
- titleL.font = [UIFont systemFontOfSize:15];
- titleL.textColor = [UIColor whiteColor];
- titleL.textAlignment = NSTextAlignmentLeft;
-
- [view addSubview:titleL];
- [view addSubview:imageView];
-
- if ([title isEqualToString:@"历史记录"]) {
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- btn.frame = CGRectMake(KScreenWidth - 45, 10, 28, 30);
- [btn setImage:[UIImage imageNamed:@"vqu_images_chatroom_sort_recycle"] forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(youpaifclearnSearchHistory:) forControlEvents:UIControlEventTouchUpInside];
- [view addSubview:btn];
- }
-
- CGFloat y = 10 + 40;
- CGFloat letfWidth = 15;
- for (int i = 0; i < textArr.count; i++) {
- NSString *text = textArr[i];
- CGFloat width = [self youpaifgetWidthWithStr:text] + 30;
- if (letfWidth + width + 15 > KScreenWidth) {
- if (y >= 130 && [title isEqualToString:@"历史记录"]) {
- [self youpaifremoveTestDataWithTextArr:textArr index:i];
- break;
- }
- y += 40;
- letfWidth = 15;
- }
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(letfWidth, y, width, 30)];
- label.userInteractionEnabled = YES;
- label.font = [UIFont systemFontOfSize:12];
- label.text = text;
- label.textColor = [UIColor whiteColor];
- label.layer.cornerRadius = 12.5;
- label.layer.masksToBounds = YES;
- label.textAlignment = NSTextAlignmentCenter;
- label.backgroundColor = HexColorFromRGB(0x4F4B5B);
- [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(youpaiftagDidCLick:)]];
- [view addSubview:label];
- letfWidth += width + 10;
- }
- view.frame = CGRectMake(0, riginY, KScreenWidth, y + 40);
- return view;
- }
- - (void)youpaiftagDidCLick:(UITapGestureRecognizer *)tap
- {
- UILabel *label = (UILabel *)tap.view;
- if (self.youpaiptapAction) {
- self.youpaiptapAction(label.text);
- }
- }
- - (void)youpaifclearnSearchHistory:(UIButton *)sender
- {
- [self.youpaipsearchHistoryView removeFromSuperview];
- self.youpaipsearchHistoryView = [self youpaifsetNoHistoryView];
- [_youpaiphistoryArray removeAllObjects];
- [NSKeyedArchiver archiveRootObject:_youpaiphistoryArray toFile:KHistorySearchPath];
- [self addSubview:self.youpaipsearchHistoryView];
-
- }
- - (UIView *)youpaifsetNoHistoryView
- {
- UIView *historyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 80)];
-
- UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 20, 14, 14)];
- imageView.image = [UIImage imageNamed:@"vqu_images_chatroom_search_his"];
- UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+6, 10, KScreenWidth - 30, 30)];
- titleL.text = @"历史记录";
- titleL.font = [UIFont systemFontOfSize:15];
- titleL.textColor = [UIColor whiteColor];
- titleL.textAlignment = NSTextAlignmentLeft;
-
- UILabel *youpaipnotextL = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(titleL.frame) + 10, 100, 20)];
- youpaipnotextL.text = @"无搜索历史";
- youpaipnotextL.font = [UIFont systemFontOfSize:12];
- youpaipnotextL.textColor = [UIColor whiteColor];
- youpaipnotextL.textAlignment = NSTextAlignmentLeft;
- [historyView addSubview:titleL];
- [historyView addSubview:imageView];
- [historyView addSubview:youpaipnotextL];
- return historyView;
- }
- - (CGFloat)youpaifgetWidthWithStr:(NSString *)text
- {
- CGFloat width = [text boundingRectWithSize:CGSizeMake(KScreenWidth, 40) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12]} context:nil].size.width;
- return width;
- }
- - (void)youpaifremoveTestDataWithTextArr:(NSMutableArray *)testArr index:(int)index
- {
- NSRange range = {index, testArr.count - index - 1};
- [testArr removeObjectsInRange:range];
- [NSKeyedArchiver archiveRootObject:testArr toFile:KHistorySearchPath];
- }
- @end
|