-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDSearchBar.m
More file actions
76 lines (58 loc) · 1.88 KB
/
JDSearchBar.m
File metadata and controls
76 lines (58 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// JDSearchBar.m
// MarvelApp
//
// Created by Юрий Логинов on 24.06.17.
// Copyright © 2017 Юрий Логинов. All rights reserved.
//
#import "JDSearchBar.h"
#import "UIColor+Colors.h"
@interface JDSearchBar()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *closeButton;
@end
@implementation JDSearchBar
+ (instancetype)searchBar {
JDSearchBar *searchBar = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
return searchBar;
}
- (void)setFilledState:(BOOL)isFilled {
if (isFilled) {
self.textField.textAlignment = NSTextAlignmentLeft;
self.closeButton.hidden = NO;
self.backgroundColor = [UIColor whiteColor];
} else {
self.textField.textAlignment = NSTextAlignmentCenter;
self.closeButton.hidden = YES;
self.backgroundColor = [UIColor jd_grayColor];
}
}
#pragma mark - Accessors
- (NSString *)searchText {
return self.textField.text;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self setFilledState:YES];
[self.delegate searchViewDidBeginEditing:self];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self.delegate searchView:self didEnterText:text];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.textField resignFirstResponder];
return YES;
}
#pragma mark - Actions
- (void)cancelSearch {
[self closeTap:nil];
}
- (IBAction)closeTap:(id)sender {
[self.delegate searchViewDidCloseTap:self];
self.textField.text = @"";
[self.textField resignFirstResponder];
[self setFilledState:NO];
}
@end