// // YOUPAIHRSearchView.m // VQU // // Created by xiaohaoran on 2021/11/9. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAIHRSearchView.h" @interface YOUPAIHRSearchView () @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