// // TFHotspotSearchView.m // TFReader // // Created by 谢腾飞 on 2020/12/24. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFHotspotSearchView.h" @interface TFHotspotSearchView () @property (nonatomic ,assign) CGRect hotspotSearchViewFrame; @end @implementation TFHotspotSearchView - (instancetype)init { if (self = [super init]) { } return self; } - (void)setHotspotBookArray:(NSArray *)hotspotBookArray { _hotspotBookArray = hotspotBookArray; UILabel *headTitle = [[UILabel alloc] init]; headTitle.textColor = kGrayTextColor; headTitle.backgroundColor = [UIColor clearColor]; headTitle.text = TFLocalizedString(@"热门搜索"); headTitle.font = kFont12; [self addSubview:headTitle]; [headTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kMargin); make.top.mas_equalTo(kHalfMargin + 5); make.width.mas_equalTo(SCREEN_WIDTH / 2); make.height.mas_equalTo(20); }]; int buttonNum = 2; // 每行多少按钮 CGFloat button_W = SCREEN_WIDTH / 2; // 按钮宽 CGFloat margin_Y = 2 * kMargin; // 第一个按钮的Y坐标 CGFloat button_H = 35; // 按钮高 CGFloat button_Y = 0; for (int i = 0; i < hotspotBookArray.count; i++) { int row = i / buttonNum; // 行号 int loc = i % buttonNum; // 列号 CGFloat button_X = button_W * loc; button_Y = margin_Y + button_H * row; UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom]; bottomButton.frame = CGRectMake(button_X, button_Y, button_W, button_H); bottomButton.backgroundColor = [UIColor clearColor]; bottomButton.tag = i; [bottomButton addTarget:self action:@selector(hotspotBookClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:bottomButton]; UILabel *indexLabel = [[UILabel alloc] init]; indexLabel.backgroundColor = kColorRGBA(203, 204, 204, 1); indexLabel.text = [NSString stringWithFormat:@"%d", i + 1]; indexLabel.font = kFont8; indexLabel.layer.cornerRadius = 2; indexLabel.clipsToBounds = YES; indexLabel.textColor = [UIColor whiteColor]; indexLabel.textAlignment = NSTextAlignmentCenter; [bottomButton addSubview:indexLabel]; switch (i) { case 0: indexLabel.backgroundColor = kColorRGBA(227, 58, 52, 1); break; case 1: indexLabel.backgroundColor = kColorRGBA(238, 132, 55, 1); break; case 2: indexLabel.backgroundColor = kColorRGBA(237, 173, 72, 1); break; default: break; } [indexLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kMargin); make.centerY.mas_equalTo(bottomButton.mas_centerY); make.height.width.mas_equalTo(13); }]; UILabel *hotWordLabel = [[UILabel alloc] init]; hotWordLabel.textAlignment = NSTextAlignmentLeft; hotWordLabel.textColor = kBlackColor; hotWordLabel.font = kMainFont; hotWordLabel.text = [hotspotBookArray objectOrNilAtIndex:i]; [bottomButton addSubview:hotWordLabel]; [hotWordLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(indexLabel.mas_right).with.offset(8); make.centerY.mas_equalTo(indexLabel.mas_centerY); make.right.mas_equalTo(bottomButton.mas_right); make.height.mas_equalTo(bottomButton.mas_height); }]; } self.hotspotSearchViewFrame = CGRectMake(0, 0, SCREEN_WIDTH, button_Y + button_H + kMargin); self.frame = self.hotspotSearchViewFrame; } - (void)hotspotBookClick:(UIButton *)sender { NSString *bookName = [self.hotspotBookArray objectOrNilAtIndex:sender.tag]; if (self.bookClickBlock) { self.bookClickBlock(bookName); } } - (void)setSmallFrame { self.frame = CGRectMake(0, 0, 0, 0); } - (void)setNormalFrame { self.frame = self.hotspotSearchViewFrame; } @end