// // HSQSearchTitleView.m // YouHuiProject // // Created by 小花 on 2018/2/6. // Copyright © 2018年 kuxuan. All rights reserved. // #import "HSQSearchTitleView.h" #import "MarqueeLabel.h" typedef void (^clickBlock)(void); typedef void (^closeBlock)(void); @interface HSQSearchTitleView () @property (nonatomic, copy) clickBlock clickblock; @end @implementation HSQSearchTitleView - (instancetype)initWithFrame:(CGRect)frame searchName:(NSString *)searchName click:(void (^)(void))clickBlock{ self = [super initWithFrame:frame]; if (self) { self.clickblock = clickBlock; self.backgroundColor = [UIColor YHColorWithHex:0xeeeeee]; self.layer.cornerRadius = frame.size.height/2; UIImageView *searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 20, 20)]; searchIcon.image = [UIImage imageNamed:@"search_gray_small"]; [self addSubview:searchIcon]; searchIcon.centerY = self.centerY; NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",searchName]]; [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attStr.length)]; [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attStr.length)]; NSTextAttachment *attach = [[NSTextAttachment alloc] init]; attach.image = [UIImage imageNamed:@"text_close"]; attach.bounds = CGRectMake(0, 0, 7, 7); [attStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:attach] atIndex:attStr.length]; CGSize size = [PublicFunction getAutoWidthWith:searchName andSize:CGSizeMake(MAXFLOAT, 25) andFont:14]; if (size.width > self.width-150) { size.width = self.width-150; } MarqueeLabel *searchText = [[MarqueeLabel alloc] initWithFrame:CGRectMake(40, 0, size.width+35, 25)]; searchText.attributedText = attStr; searchText.backgroundColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1]; searchText.layer.cornerRadius = 3; searchText.layer.masksToBounds = YES; searchText.centerY = self.centerY; searchText.textAlignment = NSTextAlignmentCenter; searchText.scrollDuration = 8.0; searchText.fadeLength = .0f; searchText.trailingBuffer = 10.f; searchText.animationDelay = 2.0f; searchText.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction:)]; [searchText addGestureRecognizer:tap]; [self addSubview:searchText]; UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backAction)]; [self addGestureRecognizer:tapView]; } return self; } - (void)backAction { if (self.clickblock) { self.clickblock(); } } - (void)closeAction:(UIButton *)sender { if (self.closeBlock) { self.closeBlock(); } } - (CGSize)intrinsicContentSize { return CGSizeMake(200, 40); } @end