-
Notifications
You must be signed in to change notification settings - Fork 20
/
AMTextFieldNumberPad.m
362 lines (295 loc) · 12.8 KB
/
AMTextFieldNumberPad.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
//
// AMTextFieldNumberPad.m
// KeyboardNumberPad
//
// Created by Vinogradov Sergey on 13.06.11.
// Copyright 2011 AppMake.Ru. All rights reserved.
//
#import "AMTextFieldNumberPad.h"
@interface AMTextFieldNumberPad (Private)
- (void)setup;
- (id)getKeyboardWindow;
- (void)checkKeyboardButton;
- (void)removeKeyboardButton;
- (void)changeButtonParams;
- (void)orientationDidChange:(NSNotification *)theNotification;
@end
@implementation AMTextFieldNumberPad
@synthesize buttonText, buttonImage, buttonIcon;
#pragma mark -
#pragma mark Initializate
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setup];
}
return self;
}
- (id)init {
if ((self = [super init])) {
[self setup];
}
return self;
}
- (void)awakeFromNib {
[self setup];
}
- (void)setup {
// Регистрируем события изменения состояния клавиатуры, UITextField и изменения ориентации экрана
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[self orientationDidChange:nil];
buttonIcon = ButtonIconNone;
}
#pragma mark -
#pragma mark Actions
- (void)actionKeyboardHide:(UIButton *)sender {
[self resignFirstResponder];
if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldShouldReturn:)])
[self.delegate performSelector:@selector(textFieldShouldReturn:) withObject:self];
}
#pragma mark -
#pragma mark Setters
- (void)setButtonText:(NSString *)theButtonText {
[theButtonText retain];
[buttonText release];
buttonText = theButtonText;
buttonIcon = ButtonIconNone;
}
- (void)setButtonImage:(UIImage *)theButtonImage {
[theButtonImage retain];
[buttonImage release];
buttonImage = theButtonImage;
buttonIcon = ButtonIconCustom;
}
- (void)setButtonIcon:(ButtonIcon)theButtonIcon {
buttonIcon = theButtonIcon;
}
#pragma mark -
#pragma mark Methods
- (id)getKeyboardWindow {
UIWindow *windowTemp = nil;
for (UIWindow *w in [[UIApplication sharedApplication] windows]) {
if ([NSStringFromClass([w class]) isEqualToString:@"UITextEffectsWindow"]) {
windowTemp = w;
break;
}
}
return windowTemp;
}
- (void)checkKeyboardButton {
// Пробуем найти нужное окно
id windowTemp = [self getKeyboardWindow];
// Если нужное окно найдено
if (windowTemp) {
// Пробуем удалить кнопку (мы ее могли создать раньше, в этом или другом UITextField)
for (UIView *v in [windowTemp subviews]) {
if ([v isKindOfClass:[UIButton class]]) {
[v setHidden:FALSE];
buttonDone = [(UIButton *)v retain];
[buttonDone removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
}
}
// Создаем кнопку если она еще не создана
if (!buttonDone) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setAdjustsImageWhenHighlighted:NO];
[[button titleLabel] setLineBreakMode:UILineBreakModeWordWrap];
[[button titleLabel] setNumberOfLines:0];
[[button titleLabel] setTextAlignment:UITextAlignmentCenter];
[button retain];
[windowTemp addSubview:button];
buttonDone = button;
[button release];
}
// Устанавливаем позицию кнопки
[self changeButtonParams];
// Добавляем событие
[buttonDone addTarget:self action:@selector(actionKeyboardHide:) forControlEvents:UIControlEventTouchUpInside];
// Если клавиатура обычная (UIKeyboardAppearanceDefault)
if (self.keyboardAppearance == UIKeyboardAppearanceDefault) {
if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard3.png"] forState:UIControlStateNormal];
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard3_highlighted.png"] forState:UIControlStateHighlighted];
}
else {
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard4.png"] forState:UIControlStateNormal];
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard4_highlighted.png"] forState:UIControlStateHighlighted];
}
}
// Если клавиатура прозрачная (UIKeyboardAppearanceAlert)
else {
if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard3_transparent.png"] forState:UIControlStateNormal];
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard3_transparent_highlighted.png"] forState:UIControlStateHighlighted];
}
else {
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard4_transparent.png"] forState:UIControlStateNormal];
[buttonDone setBackgroundImage:[UIImage imageNamed:@"AMTextFieldNumberPad.bundle/button_keyboard4_transparent_highlighted.png"] forState:UIControlStateHighlighted];
}
}
// Добавляем текст если он есть
if (buttonIcon == ButtonIconNone) {
if (self.keyboardAppearance == UIKeyboardAppearanceDefault) {
[buttonDone setTitleColor:[UIColor colorWithRed:71.0/255.0 green:78.0/255.0 blue:91.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[buttonDone setTitleColor:[UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0] forState:UIControlStateHighlighted];
[buttonDone setTitleShadowColor:[UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:0.5] forState:UIControlStateNormal];
[buttonDone setTitleShadowColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5] forState:UIControlStateHighlighted];
[[buttonDone titleLabel] setShadowOffset:CGSizeMake(0.00f, 1.00f)];
}
else {
[buttonDone setTitleColor:[UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0] forState:UIControlStateNormal];
[buttonDone setTitleColor:[UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0] forState:UIControlStateHighlighted];
[buttonDone setTitleShadowColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5] forState:UIControlStateNormal];
[buttonDone setTitleShadowColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5] forState:UIControlStateHighlighted];
[[buttonDone titleLabel] setShadowOffset:CGSizeMake(0.00f, -1.00f)];
}
[buttonDone setImage:nil forState:UIControlStateNormal];
[buttonDone setImage:nil forState:UIControlStateHighlighted];
[[buttonDone titleLabel] setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0]];
[buttonDone setReversesTitleShadowWhenHighlighted:TRUE];
// Если текст уже определен
if (buttonText)
[buttonDone setTitle:buttonText forState:UIControlStateNormal];
// Пытаемся подобрать текст из UIReturnKeyType
else {
NSString *title = nil;
if (self.returnKeyType == UIReturnKeyDone)
title = @"DONE";
else if (self.returnKeyType == UIReturnKeyEmergencyCall)
title = @"EMERGENCY CALL";
else if (self.returnKeyType == UIReturnKeyGo)
title = @"GO";
else if (self.returnKeyType == UIReturnKeyGoogle)
title = @"GOOGLE";
else if (self.returnKeyType == UIReturnKeyJoin)
title = @"JOIN";
else if (self.returnKeyType == UIReturnKeyNext)
title = @"NEXT";
else if (self.returnKeyType == UIReturnKeyRoute)
title = @"ROUTE";
else if (self.returnKeyType == UIReturnKeySearch)
title = @"SEARCH";
else if (self.returnKeyType == UIReturnKeySend)
title = @"SEND";
else if (self.returnKeyType == UIReturnKeyYahoo)
title = @"YAHOO";
else
title = @"RETURN";
[buttonDone setTitle:NSLocalizedString(title, @"") forState:UIControlStateNormal];
}
}
else if (buttonIcon == ButtonIconKeyboard) {
[buttonDone setTitle:@"" forState:UIControlStateNormal];
[buttonDone setImage:[UIImage imageNamed:((self.keyboardAppearance == UIKeyboardAppearanceDefault) ? @"AMTextFieldNumberPad.bundle/button_keyboard_icon.png" : @"AMTextFieldNumberPad.bundle/button_keyboard_icon_transparent.png")] forState:UIControlStateNormal];
[buttonDone setImage:[UIImage imageNamed:((self.keyboardAppearance == UIKeyboardAppearanceDefault) ? @"AMTextFieldNumberPad.bundle/button_keyboard_icon_highlighted.png" : @"AMTextFieldNumberPad.bundle/button_keyboard_icon_transparent_highlighted.png")] forState:UIControlStateHighlighted];
}
else if (buttonIcon == ButtonIconCustom && buttonImage) {
[buttonDone setTitle:@"" forState:UIControlStateNormal];
[buttonDone setImage:buttonImage forState:UIControlStateNormal];
[buttonDone setImage:nil forState:UIControlStateHighlighted];
}
}
}
- (void)removeKeyboardButton {
// Пробуем найти нужное окно
id windowTemp = [self getKeyboardWindow];
// Если нужное окно найдено
if (windowTemp) {
// Пробуем удалить кнопку
for (UIView *v in [windowTemp subviews]) {
if ([v isKindOfClass:[UIButton class]]) {
[v setHidden:TRUE];
}
}
}
}
- (void)changeButtonParams {
if (self.editing) {
[buttonDone setFrame:buttonRectShow];
}
}
#pragma mark -
#pragma mark Notifications
- (void)orientationDidChange:(NSNotification *)theNotification {
isRotating = TRUE;
NSUInteger orientation = [UIDevice currentDevice].orientation;
if (!UIDeviceOrientationIsValidInterfaceOrientation(orientation)) {
isPortrait = YES;
}
else {
isPortrait = UIDeviceOrientationIsPortrait(orientation);
}
buttonRectShow = (isPortrait) ? CGRectMake(0.00f, 427.00f, 105.00f, 53.00f) : CGRectMake(0.00f, 281.00f, 158.00f, 39.00f);
buttonRectHide = (isPortrait) ? CGRectMake(0.00f, 644.00f, 105.00f, 53.00f) : CGRectMake(0.00f, 443.00f, 158.00f, 39.00f);
[self changeButtonParams];
}
- (void)didBeginEditing:(NSNotification *)theNotification {
if (self.editing) {
if (self.keyboardType == UIKeyboardTypeNumberPad)
[self checkKeyboardButton];
else
[buttonDone setHidden:TRUE];
}
}
- (void)didEndEditing:(NSNotification *)theNotification {
if (!self.editing) {
[self removeKeyboardButton];
}
}
- (void)keyboardWillShow:(NSNotification *)theNotification {
isRotating = FALSE;
if (!self.editing || self.keyboardType != UIKeyboardTypeNumberPad || isKeyboardShow) {
return;
}
// Получаем нужное окно
UIWindow *windowTemp = [self getKeyboardWindow];
// Проверяем, создана ли кнопка
[self checkKeyboardButton];
// Если нужное окно найдено и кнопка существует
if (windowTemp && buttonDone) {
// Включаем отображение кнопки (на тот случай если была выключена)
[buttonDone setHidden:FALSE];
// Тянем кнопку вверх вместе с клавиатурой
[buttonDone setFrame:buttonRectHide];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[buttonDone setFrame:buttonRectShow];
[UIView commitAnimations];
}
}
- (void)keyboardWillHide:(NSNotification *)theNotification {
if (!self.editing || isRotating) {
return;
}
// Получаем нужное окн
UIWindow *windowTemp = [self getKeyboardWindow];
// Проверяем, создана ли кнопка
[self checkKeyboardButton];
// Если нужное окно найдено и кнопка существует
if (windowTemp && buttonDone) {
// Тянем кнопку вниз вместе с клавиатурой
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(buttonAnimationHide)];
[buttonDone setFrame:buttonRectHide];
[UIView commitAnimations];
}
}
- (void)buttonAnimationHide {
isKeyboardShow = FALSE;
[buttonDone setHidden:TRUE];
}
#pragma mark -
#pragma mark Memory managment
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[buttonDone release];
[buttonText release];
[buttonImage release];
[super dealloc];
}
@end