天天省钱快报

KBSearchTitleView.m 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // KBSearchTitleView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/2/6.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBSearchTitleView.h"
  9. #import "MarqueeLabel.h"
  10. typedef void (^clickBlock)(void);
  11. typedef void (^closeBlock)(void);
  12. @interface KBSearchTitleView ()
  13. @property (nonatomic, copy) clickBlock clickblock;
  14. @end
  15. @implementation KBSearchTitleView
  16. - (instancetype)initWithFrame:(CGRect)frame searchName:(NSString *)searchName click:(void (^)(void))clickBlock{
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.clickblock = clickBlock;
  20. self.backgroundColor = [UIColor YHColorWithHex:0xeeeeee];
  21. self.layer.cornerRadius = frame.size.height/2;
  22. UIImageView *searchIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 20, 20)];
  23. searchIcon.image = [UIImage imageNamed:@"search_gray_small"];
  24. [self addSubview:searchIcon];
  25. searchIcon.centerY = self.centerY;
  26. NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",searchName]];
  27. [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attStr.length)];
  28. [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attStr.length)];
  29. NSTextAttachment *attach = [[NSTextAttachment alloc] init];
  30. attach.image = [UIImage imageNamed:@"text_close"];
  31. attach.bounds = CGRectMake(0, 0, 7, 7);
  32. [attStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:attach] atIndex:attStr.length];
  33. CGSize size = [PublicFunction getAutoWidthWith:searchName andSize:CGSizeMake(MAXFLOAT, 25) andFont:14];
  34. if (size.width > self.width-150) {
  35. size.width = self.width-150;
  36. }
  37. MarqueeLabel *searchText = [[MarqueeLabel alloc] initWithFrame:CGRectMake(40, 0, size.width+35, 25)];
  38. searchText.attributedText = attStr;
  39. searchText.backgroundColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
  40. searchText.layer.cornerRadius = 3;
  41. searchText.layer.masksToBounds = YES;
  42. searchText.centerY = self.centerY;
  43. searchText.textAlignment = NSTextAlignmentCenter;
  44. searchText.scrollDuration = 8.0;
  45. searchText.fadeLength = .0f;
  46. searchText.trailingBuffer = 10.f;
  47. searchText.animationDelay = 2.0f;
  48. searchText.userInteractionEnabled = YES;
  49. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction:)];
  50. [searchText addGestureRecognizer:tap];
  51. [self addSubview:searchText];
  52. UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backAction)];
  53. [self addGestureRecognizer:tapView];
  54. }
  55. return self;
  56. }
  57. - (void)backAction {
  58. if (self.clickblock) {
  59. self.clickblock();
  60. }
  61. }
  62. - (void)closeAction:(UIButton *)sender {
  63. if (self.closeBlock) {
  64. self.closeBlock();
  65. }
  66. }
  67. - (CGSize)intrinsicContentSize
  68. {
  69. return CGSizeMake(200, 40);
  70. }
  71. @end