//
|
// SearchTitleView.m
|
// BuWanVideo2.0
|
//
|
// Created by Aeline on 2020/9/19.
|
// Copyright © 2020 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "SearchTitleView.h"
|
|
@interface SearchTitleView ()
|
|
@property (nonatomic, strong) UIScrollView *scrollView;
|
|
@property (nonatomic, strong) UIButton *buttonLast;
|
|
@property (nonatomic, strong) UIView *line;
|
@end
|
|
@implementation SearchTitleView
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
self = [super initWithFrame:frame];
|
if (self) {
|
self.backgroundColor = UIColorFromRGBValue(0x000000);
|
[self setupViewConifg];
|
}
|
return self;
|
}
|
|
- (void)setupViewConifg {
|
[self addSubview:self.scrollView];
|
}
|
|
- (void)setTitleData:(NSArray *)typeList :(BOOL)isSearch {
|
self.typeList = typeList;
|
if (typeList) {
|
//if ([[self.scrollView subviews] count] > 1) return;
|
if (!isSearch) return;
|
|
for (UIView *view in [self.scrollView subviews]) {
|
[view removeFromSuperview];
|
}
|
|
CGFloat scrWidth = 0;
|
for (int i = 0; i < typeList.count; i++) {
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
[button setTitle:typeList[i][@"Name"] forState:UIControlStateNormal];
|
[button setTitleColor:UIColorFromRGBValue(0x999999) forState:UIControlStateNormal];
|
[button setTitleColor:UIColorFromRGBValue(0xFFFFFF) forState:UIControlStateSelected];
|
button.titleLabel.font = [UIFont systemFontOfSize:12];
|
button.tag = i;
|
[self.scrollView addSubview:button];
|
|
if (i == 0) {
|
button.selected = YES;
|
self.buttonLast = button;
|
}
|
CGFloat width = 0;
|
width = [self sizeWithText:typeList[i][@"Name"] font:[UIFont boldSystemFontOfSize:12] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
|
|
if (i == 0) {
|
scrWidth = 17;
|
}
|
|
button.frame = CGRectMake(scrWidth, 5, width, 30);
|
|
scrWidth += width + 25;
|
|
[button addTarget:self action:@selector(touchIndex:) forControlEvents:UIControlEventTouchUpInside];
|
|
if (i == 0) {
|
button.transform = CGAffineTransformMakeScale(1.5, 1.5);
|
}
|
|
}
|
[self.scrollView addSubview:self.line];
|
UIButton *button = [self.scrollView subviews][1];
|
self.line.frame = CGRectMake(11, 38, button.bounds.size.width, 2);
|
self.scrollView.contentSize = CGSizeMake(scrWidth, 40);
|
}
|
}
|
|
- (void)touchIndex:(UIButton *)button {
|
|
if (button.tag == self.buttonLast.tag) return;
|
self.buttonLast.transform = CGAffineTransformMakeScale(1.0, 1.0);
|
self.buttonLast.selected = NO;
|
//self.buttonLast.titleLabel.font = [UIFont systemFontOfSize:12];
|
|
button.transform = CGAffineTransformMakeScale(1.5, 1.5);
|
button.selected = YES;
|
// button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
|
|
|
// CGFloat width1 = [self sizeWithText:self.buttonLast.titleLabel.text font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
|
//
|
//
|
// CGFloat width2 = [self sizeWithText:button.titleLabel.text font:[UIFont boldSystemFontOfSize:17] maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width;
|
//
|
//
|
// self.buttonLast.frame = CGRectMake(self.buttonLast.frame.origin.x, 5, width1, 30);
|
//
|
// button.frame = CGRectMake(button.frame.origin.x, 5, width2, 30);
|
|
self.buttonLast = button;
|
__weak typeof(self) weakSelf = self;
|
[UIView animateWithDuration:0.2 animations:^{
|
if ([_typeList[button.tag][@"Name"] length] >= 3) {
|
weakSelf.line.frame = CGRectMake(button.frame.origin.x, 38, button.bounds.size.width + 20, 2);
|
|
} else {
|
weakSelf.line.frame = CGRectMake(button.frame.origin.x, 38, button.bounds.size.width + 10, 2);
|
}
|
|
}];
|
|
if (_delegate && [_delegate respondsToSelector:@selector(selectType:)]) {
|
[_delegate selectType:[_typeList[button.tag][@"Id"] integerValue]];
|
}
|
}
|
|
- (UIScrollView *)scrollView {
|
if (!_scrollView) {
|
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 40)];
|
_scrollView.showsHorizontalScrollIndicator = NO;
|
}
|
return _scrollView;
|
}
|
|
- (UIView *)line {
|
if (!_line) {
|
_line = [[UIView alloc] init];
|
_line.backgroundColor = UIColorFromRGBValue(0xFFE84E);
|
}
|
return _line;
|
}
|
|
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {
|
NSDictionary *attrs = @{NSFontAttributeName:font};
|
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
|
}
|
|
@end
|