InfoSearchView.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // InfoSearchView.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/20.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "InfoSearchView.h"
  9. @interface InfoSearchView () <UISearchBarDelegate>
  10. @property (nonatomic, strong) TDButton *backButton;
  11. @property (nonatomic, strong) TDButton *cancelButton;
  12. @property (nonatomic, strong) UIView *lineView;
  13. @end
  14. @implementation InfoSearchView
  15. - (instancetype)initWithFrame:(CGRect)frame type:(InfoSearchViewType)type
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.backButton.frame = CGRectMake(16, (frame.size.height-14)/2.0, 7, 14);
  20. self.searchBar.frame = CGRectMake(16+7+16, 7, kGXScreenWidth-32-16-7, 30);
  21. [self addSubview:self.backButton];
  22. [self addSubview:self.searchBar];
  23. if (type == InfoSearchViewTypeCancel) {
  24. CGFloat rightMargin = 16.f;
  25. CGSize btnSize = [self.cancelButton sizeThatFits:CGSizeZero];
  26. self.cancelButton.frame = CGRectMake(kGXScreenWidth - rightMargin - btnSize.width, (frame.size.height-btnSize.height)/2.0, btnSize.width, btnSize.height);
  27. [self addSubview:self.cancelButton];
  28. rightMargin += btnSize.width + 16.f;
  29. self.searchBar.frame = CGRectMake(16+7+16, 7, kGXScreenWidth-32-7-rightMargin, 30);
  30. }
  31. // [self.searchBar becomeFirstResponder];
  32. }
  33. return self;
  34. }
  35. - (void)setNowSearchData:(NSString *)searchStr {
  36. _searchBar.text = searchStr;
  37. }
  38. - (void)showLineView {
  39. [self addSubview:self.lineView];
  40. CGRect frame = self.frame;
  41. _lineView.frame = CGRectMake(0, frame.size.height - 1, kGXScreenWidth, 0.5);
  42. }
  43. - (void)backAction:(TDButton *)sender {
  44. if (self.backBlock) {
  45. self.backBlock();
  46. }
  47. }
  48. - (void)cancelAction:(TDButton *)sender {
  49. if (self.cancelBlock) {
  50. self.cancelBlock();
  51. }
  52. }
  53. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
  54. if ([self.delegate respondsToSelector:@selector(textDidChange:)]) {
  55. [self.delegate textDidChange:searchText];
  56. }
  57. }
  58. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  59. if ([self.delegate respondsToSelector:@selector(searchButtonAction:)]) {
  60. [self.delegate searchButtonAction:searchBar];
  61. }
  62. }
  63. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
  64. self.searchBar.placeholder = @"";
  65. return YES;
  66. }
  67. - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
  68. if (self.searchPlaceholder.length > 0) {
  69. self.searchBar.placeholder = self.searchPlaceholder;
  70. }else{
  71. self.searchBar.placeholder = @" 搜索";
  72. }
  73. return YES;
  74. }
  75. #pragma mark - setter
  76. - (TDButton *)backButton {
  77. if (!_backButton) {
  78. _backButton = [TDButton buttonWithType:UIButtonTypeCustom];
  79. [_backButton setCurrentButtonHotSize:CGSizeZero];
  80. [_backButton setImage:IMG(@"首页-书籍详情-返回") forState:UIControlStateNormal];
  81. [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
  82. }
  83. return _backButton;
  84. }
  85. - (TDSearchBar *)searchBar {
  86. if (!_searchBar) {
  87. _searchBar = [[TDSearchBar alloc] init];
  88. // _searchBar.placeholder = @" 搜索";
  89. _searchBar.delegate = self;
  90. }
  91. return _searchBar;
  92. }
  93. - (TDButton *)cancelButton {
  94. if (!_cancelButton) {
  95. _cancelButton = [TDButton buttonWithType:UIButtonTypeCustom];
  96. [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  97. [_cancelButton setTitleColor:UIColorHex(1682DA) forState:UIControlStateNormal];
  98. [[_cancelButton titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
  99. [_cancelButton setCurrentButtonHotSize:CGSizeZero];
  100. [_cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
  101. }
  102. return _cancelButton;
  103. }
  104. - (UIView *)lineView {
  105. if (!_lineView) {
  106. _lineView = [UIView new];
  107. _lineView.backgroundColor = UIColorHex(CCCCCC);
  108. }
  109. return _lineView;
  110. }
  111. @end