This repository has been archived by the owner on Nov 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
UIFloatLabelTextField.m
executable file
·390 lines (320 loc) · 12.1 KB
/
UIFloatLabelTextField.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
// UIFloatLabelTextField.m
// UIFloatLabelTextField
//
// Created by Arthur Sabintsev on 3/3/14.
// Copyright (c) 2014 Arthur Ariel Sabintsev. All rights reserved.
//
#import "UIFloatLabelTextField.h"
@interface UIFloatLabelTextField ()
@property (nonatomic, copy) NSString *storedText;
@property (nonatomic, strong) UIButton *clearTextFieldButton;
@property (nonatomic, assign) CGFloat xOrigin;
@end
@implementation UIFloatLabelTextField
#pragma mark - Initialization
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
[self setPlaceholder:self.placeholder];
}
return self;
}
#pragma mark - Breakdown
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidChangeNotification
object:nil];
}
#pragma mark - Setup
- (void)setup
{
// Build textField
[self setupTextField];
// Reference Apple's clearButton and add animation
[self setupClearTextFieldButton];
// Build floatLabel
[self setupFloatLabel];
// Enable default UIMenuController options
[self setupMenuController];
}
- (void)setupTextField
{
// Textfield Padding
_horizontalPadding = 5.0f;
// Text Alignment
[self setTextAlignment:NSTextAlignmentLeft];
// Enable clearButton when textField becomes firstResponder
self.clearButtonMode = UITextFieldViewModeWhileEditing;
/*
Observer for replicating `textField:shouldChangeCharactersInRange:replacementString:` UITextFieldDelegate method,
without explicitly using UITextFieldDelegate.
*/
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textDidChange:)
name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)setupClearTextFieldButton
{
// A boolean that toggles the state of the keyboard after the clear-text button is pressed.
_dismissKeyboardWhenClearingTextField = @NO;
// Create selector for Apple's built-in UITextField button - clearButton
SEL clearButtonSelector = NSSelectorFromString(@"clearButton");
// Reference clearButton getter
IMP clearButtonImplementation = [self methodForSelector:clearButtonSelector];
// Create function pointer that returns UIButton from implementation of method that contains clearButtonSelector
UIButton * (* clearButtonFunctionPointer)(id, SEL) = (UIButton *(*)(id, SEL))clearButtonImplementation;
// Set clearTextFieldButton reference to "clearButton" from clearButtonSelector
_clearTextFieldButton = clearButtonFunctionPointer(self, clearButtonSelector);
if (_clearTextFieldButton) {
// Remove all clearTextFieldButton target-actions (e.g., Apple's standard clearButton actions)
[self.clearTextFieldButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
// Add new target-action for clearTextFieldButton
[_clearTextFieldButton addTarget:self action:@selector(clearTextField) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)setupFloatLabel
{
// floatLabel
_floatLabel = [UILabel new];
_floatLabel.textColor = [UIColor blackColor];
_floatLabel.font =[UIFont boldSystemFontOfSize:12.0f];
_floatLabel.alpha = 0.0f;
[_floatLabel setCenter:CGPointMake(_xOrigin, 0.0f)];
[self addSubview:_floatLabel];
// colors
_floatLabelPassiveColor = [UIColor lightGrayColor];
_floatLabelActiveColor = [UIColor blueColor];
// animationDuration
_floatLabelShowAnimationDuration = @0.25f;
_floatLabelHideAnimationDuration = @0.05f;
}
- (void)setupMenuController
{
_pastingEnabled = @YES;
_copyingEnabled = @YES;
_cuttingEnabled = @YES;
_selectEnabled = @YES;
_selectAllEnabled = @YES;
}
#pragma mark - Animation
- (void)toggleFloatLabel:(UIFloatLabelAnimationType)animationType
{
// Placeholder
self.placeholder = (animationType == UIFloatLabelAnimationTypeShow) ? @"" : [_floatLabel text];
// Reference textAlignment to reset origin of textField and floatLabel
_floatLabel.textAlignment = self.textAlignment = [self textAlignment];
// Common animation parameters
UIViewAnimationOptions easingOptions = (animationType == UIFloatLabelAnimationTypeShow) ? UIViewAnimationOptionCurveEaseOut : UIViewAnimationOptionCurveEaseIn;
UIViewAnimationOptions combinedOptions = UIViewAnimationOptionBeginFromCurrentState | easingOptions;
void (^animationBlock)(void) = ^{
[self toggleFloatLabelProperties:animationType];
};
// Toggle floatLabel visibility via UIView animation
CGFloat duration = (animationType == UIFloatLabelAnimationTypeShow) ? [_floatLabelShowAnimationDuration floatValue] : [_floatLabelHideAnimationDuration floatValue];
[UIView animateWithDuration:duration
delay:0.0f
options:combinedOptions
animations:animationBlock
completion:nil];
}
- (void)animateClearingTextFieldWithArray:(NSTimer *)timer
{
// Reference textArray from NSTimer object
NSMutableArray *textArray = [timer userInfo];
/*
Remove last letter (e.g., last object in array) per method call,
and display updated/truncated textField text.
*/
if ([textArray count]) {
[textArray removeLastObject];
NSString *csvString = [textArray componentsJoinedByString:@","];
_storedText = [csvString stringByReplacingOccurrencesOfString:@"," withString:@""];
self.text = _storedText;
} else {
_storedText = nil;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
[timer invalidate];
[self toggleFloatLabel:UIFloatLabelAnimationTypeHide];
if ([_dismissKeyboardWhenClearingTextField boolValue]) {
[self resignFirstResponder];
}
}
}
#pragma mark - Helpers
- (UIEdgeInsets)floatLabelInsets
{
CGFloat top = [self.text length] ? 5.0f : 0.0f;
return UIEdgeInsetsMake(top, _horizontalPadding, 0.0f, _horizontalPadding);
}
- (void)textDidChange:(NSNotification *)notification
{
if (notification.object == self) {
if ([self.text length]) {
_storedText = [self text];
if (![_floatLabel alpha]) {
[self toggleFloatLabel:UIFloatLabelAnimationTypeShow];
}
} else {
if ([_floatLabel alpha]) {
[self toggleFloatLabel:UIFloatLabelAnimationTypeHide];
}
_storedText = @"";
}
}
}
- (void)clearTextField
{
// Call UITextFieldDelegate's 'textFieldShouldClear' method if delegate is set
if ([self.delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
BOOL shouldClear = [self.delegate textFieldShouldClear:self];
if (!shouldClear) {
return;
}
}
// Create array, where each index contains one character from textField
NSMutableArray *textArray = [@[] mutableCopy];
NSUInteger i = 0;
while (i < [_storedText length]) {
NSString *character = [_storedText substringWithRange:NSMakeRange(i, 1)];
[textArray addObject:character];
++i;
}
// Reset text before animation
self.text = _storedText;
// Calculate duraiton based on _floatLabelAnimationDuration and number letters in textField
CGFloat duration = [_floatLabelHideAnimationDuration floatValue] / [textArray count];
// Perform animation
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(animateClearingTextFieldWithArray:) userInfo:textArray repeats:YES];
}
- (void)toggleFloatLabelProperties:(UIFloatLabelAnimationType)animationType
{
_floatLabel.alpha = (animationType == UIFloatLabelAnimationTypeShow) ? 1.0f : 0.0f;
CGFloat yOrigin = (animationType == UIFloatLabelAnimationTypeShow) ? 3.0f : 0.5f * CGRectGetHeight([self frame]);
_floatLabel.frame = CGRectMake(_xOrigin,
yOrigin,
CGRectGetWidth([_floatLabel frame]),
CGRectGetHeight([_floatLabel frame]));
}
#pragma mark - UITextField (Override)
- (void)setText:(NSString *)text
{
[super setText:text];
// When textField is pre-populated, show non-animated version of floatLabel
if ([text length] && !_storedText) {
[self toggleFloatLabelProperties:UIFloatLabelAnimationTypeShow];
_floatLabel.textColor = _floatLabelPassiveColor;
}
}
- (void)setPlaceholder:(NSString *)placeholder
{
[super setPlaceholder:placeholder];
if ([placeholder length]) {
_floatLabel.text = placeholder;
}
[_floatLabel sizeToFit];
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
[super setTextAlignment:textAlignment];
switch (textAlignment) {
case NSTextAlignmentRight: {
_xOrigin = CGRectGetWidth([self frame]) - CGRectGetWidth([_floatLabel frame]) - _horizontalPadding;
} break;
case NSTextAlignmentCenter: {
_xOrigin = CGRectGetWidth([self frame])/2.0f - CGRectGetWidth([_floatLabel frame])/2.0f;
} break;
default: // NSTextAlignmentLeft, NSTextAlignmentJustified, NSTextAlignmentNatural
_xOrigin = _horizontalPadding;
break;
}
}
- (CGRect)textRectForBounds:(CGRect)bounds
{
return UIEdgeInsetsInsetRect([super textRectForBounds:bounds], [self floatLabelInsets]);
}
- (CGRect)editingRectForBounds:(CGRect)bounds
{
return UIEdgeInsetsInsetRect([super editingRectForBounds:bounds], [self floatLabelInsets]);
}
#pragma mark - UILabel (Override)
- (void)setFloatLabelFont:(UIFont *)floatLabelFont
{
_floatLabelFont = floatLabelFont;
_floatLabel.font = _floatLabelFont;
[_floatLabel sizeToFit];
}
#pragma mark - UIView (Override)
- (void)layoutSubviews
{
[super layoutSubviews];
[self setTextAlignment:[self textAlignment]];
if (![self isFirstResponder] && ![self.text length]) {
[self toggleFloatLabelProperties:UIFloatLabelAnimationTypeHide];
} else if ([self.text length]) {
[self toggleFloatLabelProperties:UIFloatLabelAnimationTypeShow];
}
}
#pragma mark - UIResponder (Override)
-(BOOL)becomeFirstResponder
{
if ([super becomeFirstResponder]) {
/*
verticalPadding must be manually set if textField was initialized
using NSAutoLayout constraints
*/
_floatLabel.textColor = _floatLabelActiveColor;
_storedText = [self text];
return YES;
} else {
return NO;
}
}
- (BOOL)resignFirstResponder
{
if ([self canResignFirstResponder]) {
if ([_floatLabel.text length]) {
_floatLabel.textColor = _floatLabelPassiveColor;
}
[super resignFirstResponder];
return YES;
} else {
return NO;
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:)) { // Toggle Pasting
return ([_pastingEnabled boolValue]) ? YES : NO;
} else if (action == @selector(copy:)) { // Toggle Copying
return ([_copyingEnabled boolValue]) ? YES : NO;
} else if (action == @selector(cut:)) { // Toggle Cutting
return ([_cuttingEnabled boolValue]) ? YES : NO;
} else if (action == @selector(select:)) { // Toggle Select
return ([_selectEnabled boolValue]) ? YES : NO;
} else if (action == @selector(selectAll:)) { // Toggle Select All
return ([_selectAllEnabled boolValue]) ? YES : NO;
}
return [super canPerformAction:action withSender:sender];
}
@end