-
Notifications
You must be signed in to change notification settings - Fork 9
/
APAutocompleteTextField.m
181 lines (139 loc) · 4.38 KB
/
APAutocompleteTextField.m
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// APAutocompleteTextField.m
// APAutocompleteTextField
//
// Created by Antol Peshkov on 13.12.13.
// Copyright (c) 2013 brainSTrainer. All rights reserved.
//
#import "APAutocompleteTextField.h"
@interface APAutocompleteTextField ()
@end
@implementation APAutocompleteTextField {
BOOL _autocompleted;
NSUInteger _lengthOriginString;
}
@synthesize selectionColor = _selectionColor;
@synthesize autocompleted = _autocompleted;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.autocorrectionType = UITextAutocorrectionTypeNo;
_autocompleted = NO;
self.selectionColor = [UIColor colorWithRed:0.8f green:0.87f blue:0.93f alpha:1.f];
}
return self;
}
- (void)applyCompletion
{
if (_autocompleted) {
NSAttributedString *notMarkedString = [[NSAttributedString alloc] initWithString:self.text];
self.attributedText = notMarkedString;
_autocompleted = NO;
}
}
- (void)removeCompletion
{
if (_autocompleted) {
NSString *notCompletedString = self.text;
if (notCompletedString.length > _lengthOriginString) {
notCompletedString = [self.text substringToIndex:_lengthOriginString];
}
NSAttributedString *notCompletedAndNotMarkedString = [[NSAttributedString alloc] initWithString:notCompletedString];
self.attributedText = notCompletedAndNotMarkedString;
_autocompleted = NO;
}
}
#pragma mark - UIKeyInput
- (void)deleteBackward
{
if (_autocompleted) {
[self removeCompletion];
}
else {
[super deleteBackward];
}
}
- (void)insertText:(NSString *)text
{
if ([self isItReturnButtonPressed:text]) {
[self handleReturnButton];
return;
}
[self removeCompletion];
[super insertText:text];
_lengthOriginString = self.text.length;
[self completeString];
}
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
CGRect caretRect = CGRectZero;
if (!_autocompleted) {
caretRect = [super caretRectForPosition:position];
}
return caretRect;
}
#pragma mark - Properties
- (void)setText:(NSString *)text
{
[self removeCompletion];
[super setText:text];
}
- (BOOL)resignFirstResponder
{
[self applyCompletion];
return [super resignFirstResponder];
}
#pragma mark - Internal
- (BOOL)isItReturnButtonPressed:(NSString *)text
{
BOOL isItReturnButton = NO;
if (text.length > 0) {
unichar symbol = [text characterAtIndex:0];
isItReturnButton = (symbol == 10);
}
return isItReturnButton;
}
- (void)handleReturnButton
{
if (_autocompleted) {
NSAttributedString *stringWithoutSelection = [[NSAttributedString alloc] initWithString:self.text];
self.attributedText = stringWithoutSelection;
_autocompleted = NO;
}
}
- (void)completeString
{
NSMutableAttributedString *completedAndMarkedString = nil;
NSString *endingString = [self endingString];
if (endingString.length > 0) {
NSString *completedString = [NSString stringWithFormat:@"%@%@", self.text, endingString];
NSRange markRange = NSMakeRange(_lengthOriginString, endingString.length);
UIColor *markColor = self.selectionColor;
completedAndMarkedString = [[NSMutableAttributedString alloc] initWithString:completedString];
[completedAndMarkedString addAttribute:NSBackgroundColorAttributeName value:markColor range:markRange];
}
if (completedAndMarkedString.length > 0) {
self.attributedText = completedAndMarkedString;
_autocompleted = YES;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self applyCompletion];
}
- (NSString *)endingString
{
NSString *endingString = nil;
if (self.text.length > 0) {
NSString *completedString = [self.delegate autocompleteTextField:self completedStringForOriginString:self.text];
if (completedString.length > 0) {
NSRange rangeOriginString = [completedString rangeOfString:self.text];
NSAssert(rangeOriginString.location == 0, @"Wrong completion string");
endingString = [completedString substringFromIndex:self.text.length];
}
}
return endingString;
}
@end