-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCHDigitInput.m
executable file
·599 lines (417 loc) · 15.6 KB
/
CHDigitInput.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//
// CHDigitInput.m
// CHDigitInputControl
//
// Created by Clemens Hammerl on 27.10.12.
// Copyright (c) 2012 Clemens Hammerl. All rights reserved.
//
#import "CHDigitInput.h"
#define kDefaultDigitPadding 5.0f
#define kDefaultPlaceHolderCharacter @"0"
#define kDefaultHighlightedBackgroundColor [UIColor blueColor]
#define kDefaultNormalBackgroundColor [UIColor greenColor]
#define kDefaultHighlightedTextColor [UIColor whiteColor]
#define kDefaultNormalTextColor [UIColor blackColor]
#define kDefaultNormalShadowOffset CGSizeMake(0.0,1.0)
#define kDefaultHighlightedShadowOffset CGSizeMake(0.0,-1.0)
#define kDefaultNormalShadowColor [UIColor clearColor]
#define kDefaultHighlightedShadowColor [UIColor clearColor]
#define kDefaultFont [UIFont boldSystemFontOfSize:30]
#define kControlBackgroundColor [UIColor clearColor]
#define kControlHighlightedBackgroundColor [UIColor clearColor]
@interface CHDigitInput (private)
-(void)setupDigitViews;
-(void)highlightIndex:(NSInteger)indexToHighlight;
-(void)unhighlightAll;
-(BOOL)setText:(NSString *)text ForIndex:(NSInteger)index;
@end
@implementation CHDigitInput
@synthesize value = _value;
@synthesize matchNumberOfDigitsWithValueLength = _matchNumberOfDigitsWithValueLength;
@synthesize numberOfDigits = _numberOfDigits;
@synthesize placeHolderCharacter = _placeHolderCharacter;
@synthesize digitPadding = _digitPadding;
@synthesize backgroundView = _backgroundView;
@synthesize digitOverlayImage = _digitOverlayImage;
@synthesize digitBackgroundImage = _digitBackgroundImage;
@synthesize backgroundColor = _backgroundColor;
@synthesize highlightedBackgroundColor = _highlightedBackgroundColor;
@synthesize digitViewBackgroundColor = _digitViewBackgroundColor;
@synthesize digitViewHighlightedBackgroundColor = _digitViewHighlightedBackgroundColor;
@synthesize digitViewTextColor = _digitViewTextColor;
@synthesize digitViewHighlightedTextColor = _digitViewHighlightedTextColor;
@synthesize digitViewFont = _digitViewFont;
@synthesize digitViewShadowColor = _digitViewShadowColor;
@synthesize digitViewShadowOffset = _digitViewShadowOffset;
@synthesize digitViewHighlightedShadowColor = _digitViewHighlightedShadowColor;
@synthesize digitViewHighlightedShadowOffset = _digitViewHighlightedShadowOffset;
/////////////////// Initialization ///////////////////
#pragma mark - Initializaion
-(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];
}
return self;
}
-(id)initWithNumberOfDigits:(NSInteger)digitCount
{
self = [self init];
if (self) {
[self setNumberOfDigits:digitCount];
}
return self;
}
-(void)redrawControl
{
[self setupDigitViews];
}
- (void)setup
{
// setting a default background view
_backgroundColor = kControlBackgroundColor;
_highlightedBackgroundColor = kControlHighlightedBackgroundColor;
_backgroundView = [[UIView alloc] init];
_backgroundView.backgroundColor = _backgroundColor;
[self addSubview:_backgroundView];
// creating the array that holds the digit views and the bg image views
digitViews = [[NSMutableArray alloc] init];
bgImageViews = [[NSMutableArray alloc] init];
// setting a dafault digitview appearance
_digitViewHighlightedBackgroundColor = kDefaultHighlightedBackgroundColor;
_digitViewBackgroundColor = kDefaultNormalBackgroundColor;
_digitViewHighlightedTextColor = kDefaultHighlightedTextColor;
_digitViewTextColor = kDefaultNormalTextColor;
_digitViewShadowColor = kDefaultNormalShadowColor;
_digitViewShadowOffset = kDefaultNormalShadowOffset;
_digitViewHighlightedShadowOffset = kDefaultHighlightedShadowOffset;
_digitViewHighlightedShadowColor = kDefaultHighlightedShadowColor;
_digitViewFont = kDefaultFont;
_matchNumberOfDigitsWithValueLength = NO;
_placeHolderCharacter = kDefaultPlaceHolderCharacter;
}
//////////////// Overriden property setters ///////////////////
#pragma mark - overriden property setters
-(void)setPlaceHolderCharacter:(NSString *)placeHolderCharacter
{
if (placeHolderCharacter.length > 1) {
NSRange range;
range.length = 1;
range.location = 0;
_placeHolderCharacter = [placeHolderCharacter substringWithRange:range];
@throw [NSException exceptionWithName:@"PlaceholderCharacterTooLong" reason:@"The Placeholder character must be an NSString with a maximum length of 1" userInfo:nil];
}else {
_placeHolderCharacter = placeHolderCharacter;
}
// redraw everything
[self setupDigitViews];
}
-(void)setMatchNumberOfDigitsWithValueLength:(BOOL)matchNumberOfDigitsWithValueLength
{
_matchNumberOfDigitsWithValueLength = matchNumberOfDigitsWithValueLength;
// redraw everthing
[self setValue:_value];
}
// Override property setter
-(void)setNumberOfDigits:(NSInteger)numberOfDigits
{
_numberOfDigits = numberOfDigits;
if (numberOfDigits > 9) {
@throw [NSException exceptionWithName:@"NSRangeExeption" reason:@"To many digits for NSInteger" userInfo:nil];
}
[self setupDigitViews];
}
// Set a new integer value
-(void)setValue:(NSInteger)value
{
// do magic for displaying
_value = value;
NSString *valueString = [NSString stringWithFormat:@"%i",value];
activeString = valueString;
int valueStringLength = valueString.length;
// if lenght of value is bigger than
// the number of digit view we set up
// a new number according to the length, but
// only if matchNumberOfDigitsWithValueLength is TRUE
if (_matchNumberOfDigitsWithValueLength) {
self.numberOfDigits = valueStringLength;
}
int padding = _numberOfDigits-valueStringLength;
if (padding < 0) {
padding = 0;
}
int currentPos = 0;
int stringIndex = 0;
for (UILabel *digitView in digitViews) {
if (currentPos >= padding) {
NSRange range;
range.location = stringIndex;
range.length = 1;
digitView.text = [valueString substringWithRange:range];
stringIndex++;
}
currentPos++;
}
}
-(void)setBackgroundView:(UIView *)backgroundView
{
[_backgroundView removeFromSuperview];
_backgroundView = backgroundView;
[self insertSubview:_backgroundView atIndex:0];
}
////////////////// Private Methods //////////////////////
#pragma mark - private methods
// generate the digit views
-(void)setupDigitViews
{
// remove the old views
for (UIView *digitView in digitViews) {
[digitView removeFromSuperview];
}
for (UIView *bgView in bgImageViews) {
[bgView removeFromSuperview];
}
[digitViews removeAllObjects];
[bgImageViews removeAllObjects];
// generate new views and set default digit to 0
for (int i = 0; i < _numberOfDigits; i++) {
UILabel *newDigitView = [[UILabel alloc] init];
// configuring the label
newDigitView.textAlignment = UITextAlignmentCenter;
newDigitView.textColor = _digitViewTextColor;
newDigitView.highlightedTextColor = _digitViewHighlightedTextColor;
newDigitView.shadowColor = _digitViewShadowColor;
newDigitView.shadowOffset = _digitViewShadowOffset;
newDigitView.backgroundColor = _digitViewBackgroundColor;
newDigitView.font = _digitViewFont;
newDigitView.adjustsFontSizeToFitWidth = YES;
newDigitView.text = _placeHolderCharacter;
// adding the custom background if available
if (_digitOverlayImage) {
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:_digitOverlayImage];
[newDigitView addSubview:overlayImageView];
}
if (_digitBackgroundImage) {
UIImageView *bgDigitView = [[UIImageView alloc] initWithImage:_digitBackgroundImage];
[self addSubview:bgDigitView];
[bgImageViews addObject:bgDigitView];
}
// adding the digit to the view and the digitview array
[self addSubview:newDigitView];
[digitViews addObject:newDigitView];
}
[self setNeedsLayout];
}
// highlights the digit view with the given index
-(void)highlightIndex:(NSInteger)indexToHighlight
{
for (int i = 0; i < [digitViews count]; i++) {
UILabel *digitView = [digitViews objectAtIndex:i];
if (i == indexToHighlight) {
digitView.backgroundColor = _digitViewHighlightedBackgroundColor;
digitView.highlighted = YES;
digitView.shadowColor = _digitViewHighlightedShadowColor;
digitView.shadowOffset = _digitViewHighlightedShadowOffset;
}else {
digitView.backgroundColor = _digitViewBackgroundColor;
digitView.shadowColor = _digitViewShadowColor;
digitView.shadowOffset = _digitViewShadowOffset;
digitView.highlighted = NO;
}
}
}
// Unhighlights all digit views
-(void)unhighlightAll
{
for (UILabel *digitView in digitViews) {
digitView.backgroundColor = _digitViewBackgroundColor;
digitView.shadowColor = _digitViewShadowColor;
digitView.shadowOffset = _digitViewShadowOffset;
digitView.highlighted = NO;
}
}
// Set text for a given digitview index
-(BOOL)setText:(NSString *)text ForIndex:(NSInteger)index
{
if (!isdigit([text characterAtIndex:0]))
return NO;
UILabel *activeDigitView = [digitViews objectAtIndex:index];
activeDigitView.text = text;
NSString *valueString = @"";
for (UILabel *digitView in digitViews) {
NSString *digitString = digitView.text;
if ([digitString isEqualToString:_placeHolderCharacter]) {
valueString = [valueString stringByAppendingString:@"0"];
}else {
valueString =[valueString stringByAppendingString:digitString];
}
}
activeString = valueString;
_value = [valueString integerValue];
[self sendActionsForControlEvents:UIControlEventEditingChanged];
[self sendActionsForControlEvents:UIControlEventValueChanged];
return YES;
}
////////////////// UIKeyInput Protocol implementation ///////////////////
#pragma mark - UIKeyInput Protocol implementation
// gets called when keyboard text is entered
- (void)insertText:(NSString *)text
{
// set the entered text in the digit view
// at the current index
if ([self setText:text ForIndex:currentIndex]) {
// automatically jump to the next digit
currentIndex++;
// if currentindex is bigger than
// number of digitviews we assume
// that we are done with entering
// digits and are hiding the keyboard
if (currentIndex >= _numberOfDigits) {
[self resignFirstResponder];
return;
}
[self highlightIndex:currentIndex];
}
}
// get called when keboard delete button is pressed
// Delete button action is just moving
// the current position on position to
// the left
- (void)deleteBackward
{
/*
for (int i = 0; i <= currentIndex; i++) {
NSLog(@"current index");
[self setText:_placeHolderCharacter ForIndex:i];
}
*/
//currentIndex = 0;
/*
if (currentIndex >= 1) {
UILabel *previousDigitView = [digitViews objectAtIndex:(currentIndex-1)];
if ([previousDigitView.text isEqualToString:_placeHolderCharacter]) {
[self setText:_placeHolderCharacter ForIndex:currentIndex];
return;
}
}
*/
currentIndex--;
if (currentIndex <= 0) {
currentIndex = 0;
}
[self highlightIndex:currentIndex];
}
-(BOOL)hasText
{
return activeString != nil;
}
///////////////////// UITextInputTraits protocol implementation ////////////////
#pragma mark - UITextInputTraits protocol implementation
// always return a number pad keyboard
-(UIKeyboardType)keyboardType
{
return UIKeyboardTypeNumberPad;
}
//////////////////// View Stuff and other internal stuff //////////////////
#pragma mark - view stuff and other internal stuff
-(void)layoutSubviews
{
[super layoutSubviews];
_backgroundView.frame = self.bounds;
CGFloat xOffset = 0;
CGFloat digitWidth = (self.bounds.size.width-((_numberOfDigits+1)*kDefaultDigitPadding)) / (CGFloat)_numberOfDigits;
for (UIView *digitView in digitViews) {
digitView.frame = CGRectMake(kDefaultDigitPadding+xOffset, kDefaultDigitPadding, digitWidth, self.bounds.size.height-2*kDefaultDigitPadding);
xOffset = digitView.frame.origin.x+digitView.bounds.size.width;
}
xOffset = 0;
// TODO: remove duplicate code and implement better solulition for setting
// frames of bgimage views
if (_digitBackgroundImage) {
for (UIView *bgDigitView in bgImageViews) {
bgDigitView.frame = CGRectMake(kDefaultDigitPadding+xOffset, kDefaultDigitPadding, digitWidth, self.bounds.size.height-2*kDefaultDigitPadding);
xOffset = bgDigitView.frame.origin.x+bgDigitView.bounds.size.width;
}
}
}
// always return yes
-(BOOL)canBecomeFirstResponder
{
[self setHighlighted:YES];
[self sendActionsForControlEvents:UIControlEventEditingDidBegin];
return YES;
}
// Unhighlight all digitviews
// and hide keyboard
-(BOOL)resignFirstResponder
{
[self unhighlightAll];
if (self.highlighted) {
[self sendActionsForControlEvents:UIControlEventEditingDidEnd];
}
[self setHighlighted:NO];
return [super resignFirstResponder];
}
// determine active index and show up keyboard
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
// TODO: ignore padding to have a bigger touch area
// Check if a touch is inside a digitview
// if so set the currentIndex and show
// up the keyboard by becoming the first
// responder
NSInteger touchedIndex = 0;
for (UIView *digitView in digitViews) {
if (CGRectContainsPoint(digitView.frame, touchPoint)) {
currentIndex = touchedIndex;
[self highlightIndex:currentIndex];
[self becomeFirstResponder];
break;
}
touchedIndex++;
}
}
//////////////// UIControl Methods //////////////////
#pragma mark - UIControl Methods
-(void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
if (enabled) {
for (UIView *digitView in digitViews) {
digitView.alpha = 1.0;
}
}else {
for (UIView *digitView in digitViews) {
digitView.alpha = 0.5;
}
}
}
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
if (highlighted) {
self.backgroundView.backgroundColor = _highlightedBackgroundColor;
}else {
self.backgroundView.backgroundColor = _backgroundColor;
}
}
@end