南锋

南奔万里空,脱死锋镝余

cocosCreator导出的ios工程调起系统键盘时,会有一个小的输入框

需求背景

在使用cocosCreator3.7.2版本导出ios工程,在点击输入框,调起系统键盘时,在键盘上方会出现一个小的输入框。如下图所示

这个是因为Cocos 的 EditBox 在 iOS 原生层一般会借助 UITextField / UITextView 来接管输入,所以这里并不是一个错误,但是我们也可以进行修改。

修改

我们这里需要修改cocosCreator引擎的源代码
找到EditBox-ios.mm文件,然后直接替换为下面代码

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
#include "EditBox.h"
#include "cocos/bindings/jswrapper/SeApi.h"
#include "cocos/bindings/manual/jsb_global.h"
#include "engine/EngineEvents.h"
#import <UIKit/UIKit.h>

#define TEXT_LINE_HEIGHT 40
#define TEXT_VIEW_MAX_LINE_SHOWN 1.5

// 是否隐藏原生输入框。
// 这里改成 false,避免真正输入框被隐藏后出现奇怪输入行为。
const bool INPUTBOX_HIDDEN = false;

/*************************************************************************
Inner class declarations.
************************************************************************/
@interface EditboxManager : NSObject
+ (instancetype)sharedInstance;
- (void)show:(const cc::EditBox::ShowInfo*)showInfo;
- (void)hide;
- (UIView*)getCurrentViewInUse;
- (NSString*)getCurrentText;
@end

@interface TextFieldDelegate : NSObject <UITextFieldDelegate>
- (id)initWithInput:(UITextField*)inputOnView;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- (void)textFieldDidChange:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
@end

@interface TextViewDelegate : NSObject <UITextViewDelegate>
- (id)initWithInput:(UITextView*)inputOnView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
@end

/*************************************************************************
Global variables and functions relative to script engine.
************************************************************************/
namespace {
static bool g_isMultiline{false};
static int g_maxLength{INT_MAX};
se::Value textInputCallback;

void getTextInputCallback() {
if (!textInputCallback.isUndefined()) {
return;
}

auto global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Value jsbVal;
if (global->getProperty("jsb", &jsbVal) && jsbVal.isObject()) {
jsbVal.toObject()->getProperty("onTextInput", &textInputCallback);
se::ScriptEngine::getInstance()->addBeforeCleanupHook([]() {
textInputCallback.setUndefined();
});
}
}

void callJSFunc(const ccstd::string &type, const ccstd::string &text) {
getTextInputCallback();

if (textInputCallback.isUndefined() || !textInputCallback.isObject()) {
return;
}

se::AutoHandleScope scope;
se::ValueArray args;
args.push_back(se::Value(type));
args.push_back(se::Value(text));
textInputCallback.toObject()->call(args, nullptr);
}

int getTextInputHeight(bool isMultiLine) {
if (isMultiLine) {
return TEXT_LINE_HEIGHT * TEXT_VIEW_MAX_LINE_SHOWN;
}
return TEXT_LINE_HEIGHT;
}

void setTextFieldKeyboardType(UITextField *textField, const ccstd::string &inputType) {
if (0 == inputType.compare("password")) {
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
} else {
textField.secureTextEntry = NO;
if (0 == inputType.compare("email")) {
textField.keyboardType = UIKeyboardTypeEmailAddress;
} else if (0 == inputType.compare("number")) {
textField.keyboardType = UIKeyboardTypeDecimalPad;
} else if (0 == inputType.compare("url")) {
textField.keyboardType = UIKeyboardTypeURL;
} else {
textField.keyboardType = UIKeyboardTypeDefault;
}
}
}

void setTextFieldReturnType(UITextField *textField, const ccstd::string &returnType) {
if (0 == returnType.compare("done")) {
textField.returnKeyType = UIReturnKeyDone;
} else if (0 == returnType.compare("next")) {
textField.returnKeyType = UIReturnKeyNext;
} else if (0 == returnType.compare("search")) {
textField.returnKeyType = UIReturnKeySearch;
} else if (0 == returnType.compare("go")) {
textField.returnKeyType = UIReturnKeyGo;
} else if (0 == returnType.compare("send")) {
textField.returnKeyType = UIReturnKeySend;
} else {
textField.returnKeyType = UIReturnKeyDone;
}
}

NSString* getTextInputType(const cc::EditBox::ShowInfo* showInfo) {
return showInfo->isMultiline ? @"textView" : @"textField";
}
} // namespace

/*************************************************************************
Class implementations.
************************************************************************/
@implementation TextViewDelegate {
UITextView* tViewOnView;
}

- (id)initWithInput:(UITextView*)inputOnView {
if (self = [super init]) {
tViewOnView = inputOnView;
}
return self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
if (textView.markedTextRange != nil) {
return;
}

if (textView.text.length > g_maxLength) {
NSRange rangeIndex = [textView.text rangeOfComposedCharacterSequenceAtIndex:g_maxLength];
textView.text = [textView.text substringToIndex:rangeIndex.location];
}

tViewOnView.text = textView.text;
callJSFunc("input", [textView.text UTF8String]);
}

@end

@implementation TextFieldDelegate {
UITextField* textFieldOnView;
}

- (id)initWithInput:(UITextField*)inputOnView {
if (self = [super init]) {
textFieldOnView = inputOnView;
}
return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return YES;
}

- (void)textFieldDidChange:(UITextField *)textField {
if (textField.markedTextRange != nil) {
return;
}

if (textField.text.length > g_maxLength) {
NSRange rangeIndex = [textField.text rangeOfComposedCharacterSequenceAtIndex:g_maxLength];
textField.text = [textField.text substringToIndex:rangeIndex.location];
}

textFieldOnView.text = textField.text;
callJSFunc("input", [textField.text UTF8String]);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
cc::EditBox::complete();
return YES;
}

@end

@interface InputBoxPair : NSObject
@property(readwrite, retain) id inputOnView;
@property(readwrite, retain) id inputDelegate;
@end

@implementation InputBoxPair

- (void)dealloc {
[_inputOnView release];
[_inputDelegate release];
[super dealloc];
}

@end

@implementation EditboxManager
{
NSMutableDictionary<NSString*, InputBoxPair*>* textInputDictionnary;
InputBoxPair* curView;

cc::events::Resize::Listener resizeListener;
cc::events::Touch::Listener touchListener;
cc::events::Close::Listener closeListener;
}

static EditboxManager *instance = nil;

+ (instancetype)sharedInstance {
static dispatch_once_t pred = 0;
dispatch_once(&pred, ^{
instance = [[super allocWithZone:NULL] init];
if (instance == nil) {
CC_LOG_ERROR("Editbox manager init failed, plz check if you have enough space left");
}
});
return instance;
}

+ (id)allocWithZone:(struct _NSZone*)zone {
return [EditboxManager sharedInstance];
}

- (id)copyWithZone:(struct _NSZone*)zone {
return [EditboxManager sharedInstance];
}

- (void)onOrientationChanged {
cc::EditBox::complete();
}

- (id)init {
if (self = [super init]) {
textInputDictionnary = [NSMutableDictionary new];
if (textInputDictionnary == nil) {
[self release];
return nil;
}

resizeListener.bind([&](int /*width*/, int /*height*/, uint32_t /*windowId*/) {
[[EditboxManager sharedInstance] onOrientationChanged];
});

touchListener.bind([&](const cc::TouchEvent& event) {
if (event.type == cc::TouchEvent::Type::BEGAN) {
cc::EditBox::complete();
}
});

closeListener.bind([&]() {
[[EditboxManager sharedInstance] dealloc];
});
}
return self;
}

- (void)dealloc {
[textInputDictionnary release];
[super dealloc];
}

- (id)createTextView:(const cc::EditBox::ShowInfo *)showInfo {
InputBoxPair* ret;
CGRect viewRect = UIApplication.sharedApplication.delegate.window.rootViewController.view.frame;
NSString* inputType = getTextInputType(showInfo);

if ((ret = [textInputDictionnary objectForKey:inputType])) {
[[ret inputOnView] setFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)];
} else {
ret = [[InputBoxPair alloc] init];

UITextView *view = [[UITextView alloc]
initWithFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)];

view.backgroundColor = [UIColor clearColor];
view.textColor = [UIColor clearColor];
view.tintColor = [UIColor clearColor];
view.autocorrectionType = UITextAutocorrectionTypeNo;
view.autocapitalizationType = UITextAutocapitalizationTypeNone;
view.scrollEnabled = NO;

[ret setInputOnView:view];
[view release];

TextViewDelegate* delegate = [[TextViewDelegate alloc] initWithInput:[ret inputOnView]];
((UITextView*)[ret inputOnView]).delegate = delegate;
ret.inputDelegate = delegate;
[delegate release];

[textInputDictionnary setValue:ret forKey:inputType];
}

((UITextView*)[ret inputOnView]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
return ret;
}

- (id)createTextField:(const cc::EditBox::ShowInfo*)showInfo {
InputBoxPair* ret;
CGRect viewRect = UIApplication.sharedApplication.delegate.window.rootViewController.view.frame;
NSString* inputType = getTextInputType(showInfo);

if ((ret = [textInputDictionnary objectForKey:inputType])) {
[[ret inputOnView] setFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)];
} else {
ret = [[InputBoxPair alloc] init];

UITextField *field = [[UITextField alloc]
initWithFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)];

field.backgroundColor = [UIColor clearColor];
field.textColor = [UIColor clearColor];
field.tintColor = [UIColor clearColor];
field.borderStyle = UITextBorderStyleNone;
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.autocapitalizationType = UITextAutocapitalizationTypeNone;
field.inputAccessoryView = nil;

[ret setInputOnView:field];
[field release];

TextFieldDelegate* delegate = [[TextFieldDelegate alloc] initWithInput:[ret inputOnView]];
((UITextField*)[ret inputOnView]).delegate = delegate;
[((UITextField*)[ret inputOnView]) addTarget:delegate
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
ret.inputDelegate = delegate;
[delegate release];

[textInputDictionnary setValue:ret forKey:inputType];
}

((UITextField*)[ret inputOnView]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
setTextFieldReturnType((UITextField*)[ret inputOnView], showInfo->confirmType);
setTextFieldKeyboardType((UITextField*)[ret inputOnView], showInfo->inputType);

return ret;
}

- (void)show:(const cc::EditBox::ShowInfo*)showInfo {
g_maxLength = showInfo->maxLength;
g_isMultiline = showInfo->isMultiline;

if (g_isMultiline) {
curView = [self createTextView:showInfo];
} else {
curView = [self createTextField:showInfo];
}

[[curView inputOnView] setHidden:INPUTBOX_HIDDEN];

UIView *view = UIApplication.sharedApplication.delegate.window.rootViewController.view;
if ([[curView inputOnView] superview] != view) {
[[curView inputOnView] removeFromSuperview];
[view addSubview:[curView inputOnView]];
}

[[curView inputOnView] becomeFirstResponder];
}

- (void)hide {
[[curView inputOnView] resignFirstResponder];
[[curView inputOnView] removeFromSuperview];
}

- (UIView*)getCurrentViewInUse {
return (UIView*)[curView inputOnView];
}

- (NSString*)getCurrentText {
if (g_isMultiline) {
return [(UITextView*)[curView inputOnView] text];
}
return [(UITextField*)[curView inputOnView] text];
}

@end

/*************************************************************************
Implementation of EditBox.
************************************************************************/

namespace cc {

bool EditBox::_isShown = false;

void EditBox::show(const cc::EditBox::ShowInfo &showInfo) {
[[EditboxManager sharedInstance] show:&showInfo];
EditBox::_isShown = true;
}

void EditBox::hide() {
[[EditboxManager sharedInstance] hide];
EditBox::_isShown = false;
}

bool EditBox::complete() {
if (!EditBox::_isShown) {
return true;
}

NSString *text = [[EditboxManager sharedInstance] getCurrentText];
callJSFunc("complete", [text UTF8String]);
EditBox::hide();

return true;
}

} // namespace cc

替换后再次运行就没有小的输入框了呀

修改输入框样式

直接替换下面代码,修改输入框样式,将输入框样式变成和屏幕一样长,且去掉done按钮

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
#include "EditBox.h"
#include "cocos/bindings/jswrapper/SeApi.h"
#include "cocos/bindings/manual/jsb_global.h"
#include "engine/EngineEvents.h"
#import <UIKit/UIKit.h>

#define ITEM_MARGIN_WIDTH 0
#define ITEM_MARGIN_HEIGHT 10
#define TEXT_LINE_HEIGHT 40
#define TEXT_VIEW_MAX_LINE_SHOWN 1.5

const bool INPUTBOX_HIDDEN = true;

/*************************************************************************
Inner class declarations.
************************************************************************/
@interface EditboxManager : NSObject
+ (instancetype)sharedInstance;
- (void)show:(const cc::EditBox::ShowInfo*)showInfo;
- (void)hide;
- (UIView*)getCurrentViewInUse;
- (NSString*)getCurrentText;
@end

@interface TextFieldDelegate : NSObject <UITextFieldDelegate>
- (id)initWithPairs:(UITextField*)inputOnView and:(UITextField*)inputOnToolbar;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- (void)textFieldDidChange:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
@end

@interface TextViewDelegate : NSObject <UITextViewDelegate>
- (id)initWithPairs:(UITextView*)inputOnView and:(UITextView*)inputOnToolbar;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
@end

/*************************************************************************
Global variables and functions relative to script engine.
************************************************************************/
namespace {
static bool g_isMultiline{false};
static int g_maxLength{INT_MAX};
se::Value textInputCallback;

void getTextInputCallback() {
if (!textInputCallback.isUndefined()) {
return;
}

auto global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Value jsbVal;
if (global->getProperty("jsb", &jsbVal) && jsbVal.isObject()) {
jsbVal.toObject()->getProperty("onTextInput", &textInputCallback);
se::ScriptEngine::getInstance()->addBeforeCleanupHook([]() {
textInputCallback.setUndefined();
});
}
}

void callJSFunc(const ccstd::string &type, const ccstd::string &text) {
getTextInputCallback();

if (textInputCallback.isUndefined() || !textInputCallback.isObject()) {
return;
}

se::AutoHandleScope scope;
se::ValueArray args;
args.push_back(se::Value(type));
args.push_back(se::Value(text));
textInputCallback.toObject()->call(args, nullptr);
}

int getTextInputHeight(bool isMultiLine) {
if (isMultiLine) {
return TEXT_LINE_HEIGHT * TEXT_VIEW_MAX_LINE_SHOWN;
}
return TEXT_LINE_HEIGHT;
}

void setTextFieldKeyboardType(UITextField *textField, const ccstd::string &inputType) {
if (0 == inputType.compare("password")) {
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
} else {
textField.secureTextEntry = NO;
if (0 == inputType.compare("email")) {
textField.keyboardType = UIKeyboardTypeEmailAddress;
} else if (0 == inputType.compare("number")) {
textField.keyboardType = UIKeyboardTypeDecimalPad;
} else if (0 == inputType.compare("url")) {
textField.keyboardType = UIKeyboardTypeURL;
} else {
textField.keyboardType = UIKeyboardTypeDefault;
}
}
}

void setTextFieldReturnType(UITextField *textField, const ccstd::string &returnType) {
if (0 == returnType.compare("done")) {
textField.returnKeyType = UIReturnKeyDone;
} else if (0 == returnType.compare("next")) {
textField.returnKeyType = UIReturnKeyNext;
} else if (0 == returnType.compare("search")) {
textField.returnKeyType = UIReturnKeySearch;
} else if (0 == returnType.compare("go")) {
textField.returnKeyType = UIReturnKeyGo;
} else if (0 == returnType.compare("send")) {
textField.returnKeyType = UIReturnKeySend;
} else {
textField.returnKeyType = UIReturnKeyDone;
}
}

CGRect getSafeAreaRect() {
UIView *view = UIApplication.sharedApplication.delegate.window.rootViewController.view;
CGRect viewRect = view.frame;

if (@available(iOS 11.0, *)) {
auto safeAreaInsets = view.safeAreaInsets;

UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationLandscapeLeft == orient) {
viewRect.origin.x = 0;
viewRect.size.width -= safeAreaInsets.right;
} else {
viewRect.origin.x += safeAreaInsets.left;
viewRect.size.width -= safeAreaInsets.left;
}
}

return viewRect;
}

NSString* getTextInputType(const cc::EditBox::ShowInfo* showInfo) {
return showInfo->isMultiline ? @"textView" : @"textField";
}
} // namespace

/*************************************************************************
Class implementations.
************************************************************************/
@implementation TextViewDelegate {
UITextView* tViewOnView;
UITextView* tViewOnToolbar;
}

- (id)initWithPairs:(UITextView*)inputOnView and:(UITextView*)inputOnToolbar {
if (self = [super init]) {
tViewOnView = inputOnView;
tViewOnToolbar = inputOnToolbar;
}
return self;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
if (textView.markedTextRange != nil) {
return;
}

if (textView.text.length > g_maxLength) {
NSRange rangeIndex = [textView.text rangeOfComposedCharacterSequenceAtIndex:g_maxLength];
textView.text = [textView.text substringToIndex:rangeIndex.location];
}

tViewOnView.text = textView.text;
tViewOnToolbar.text = textView.text;
callJSFunc("input", [textView.text UTF8String]);
}

@end

@implementation TextFieldDelegate {
UITextField* textFieldOnView;
UITextField* textFieldOnToolbar;
}

- (id)initWithPairs:(UITextField*)inputOnView and:(UITextField*)inputOnToolbar {
if (self = [super init]) {
textFieldOnView = inputOnView;
textFieldOnToolbar = inputOnToolbar;
}
return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return YES;
}

- (void)textFieldDidChange:(UITextField *)textField {
if (textField.markedTextRange != nil) {
return;
}

if (textField.text.length > g_maxLength) {
NSRange rangeIndex = [textField.text rangeOfComposedCharacterSequenceAtIndex:g_maxLength];
textField.text = [textField.text substringToIndex:rangeIndex.location];
}

textFieldOnView.text = textField.text;
textFieldOnToolbar.text = textField.text;
callJSFunc("input", [textField.text UTF8String]);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
cc::EditBox::complete();
return YES;
}

@end

@interface InputBoxPair : NSObject
@property(readwrite, retain) id inputOnView;
@property(readwrite, retain) id inputOnToolbar;
@property(readwrite, retain) id inputDelegate;
@end

@implementation InputBoxPair
- (void)dealloc {
[_inputOnView release];
[_inputOnToolbar release];
[_inputDelegate release];
[super dealloc];
}
@end

@implementation EditboxManager {
NSMutableDictionary<NSString*, InputBoxPair*>* textInputDictionnary;
InputBoxPair* curView;

cc::events::Resize::Listener resizeListener;
cc::events::Touch::Listener touchListener;
cc::events::Close::Listener closeListener;
}

static EditboxManager *instance = nil;

+ (instancetype)sharedInstance {
static dispatch_once_t pred = 0;
dispatch_once(&pred, ^{
instance = [[super allocWithZone:NULL] init];
if (instance == nil) {
CC_LOG_ERROR("Editbox manager init failed, plz check if you have enough space left");
}
});
return instance;
}

+ (id)allocWithZone:(struct _NSZone*)zone {
return [EditboxManager sharedInstance];
}

- (id)copyWithZone:(struct _NSZone*)zone {
return [EditboxManager sharedInstance];
}

- (void)onOrientationChanged {
cc::EditBox::complete();
}

- (id)init {
if (self = [super init]) {
textInputDictionnary = [NSMutableDictionary new];
if (textInputDictionnary == nil) {
[self release];
return nil;
}

resizeListener.bind([&](int /*width*/, int /*height*/, uint32_t /*windowId*/) {
[[EditboxManager sharedInstance] onOrientationChanged];
});

touchListener.bind([&](const cc::TouchEvent& event) {
if (event.type == cc::TouchEvent::Type::BEGAN) {
cc::EditBox::complete();
}
});

closeListener.bind([&]() {
[[EditboxManager sharedInstance] dealloc];
});
}
return self;
}

- (void)dealloc {
[textInputDictionnary release];
[super dealloc];
}

- (UIView *)createAccessoryContainerWithHeight:(CGFloat)height {
CGRect safeRect = getSafeAreaRect();
UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, safeRect.size.width, height)] autorelease];
container.backgroundColor = [UIColor darkGrayColor];
return container;
}

- (void)addInputAccessoryViewForTextView:(InputBoxPair*)inputbox
with:(const cc::EditBox::ShowInfo*)showInfo {
CGFloat height = getTextInputHeight(g_isMultiline) + ITEM_MARGIN_HEIGHT;
UIView *container = [self createAccessoryContainerWithHeight:height];
CGFloat width = container.frame.size.width;

UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(0,
0,
width,
getTextInputHeight(g_isMultiline))];
textView.textColor = [UIColor blackColor];
textView.backgroundColor = [UIColor whiteColor];
textView.layer.cornerRadius = 0.0;
textView.clipsToBounds = YES;
textView.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8);
textView.text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];

TextViewDelegate* delegate = [[TextViewDelegate alloc] initWithPairs:[inputbox inputOnView] and:textView];
inputbox.inputDelegate = delegate;
textView.delegate = delegate;

[container addSubview:textView];
((UITextView*)[inputbox inputOnView]).inputAccessoryView = container;
[inputbox setInputOnToolbar:textView];

[textView release];
}

- (void)addInputAccessoryViewForTextField:(InputBoxPair*)inputbox
with:(const cc::EditBox::ShowInfo*)showInfo {
CGFloat height = TEXT_LINE_HEIGHT + ITEM_MARGIN_HEIGHT;
UIView *container = [self createAccessoryContainerWithHeight:height];
CGFloat width = container.frame.size.width;

UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(0,
0,
width,
TEXT_LINE_HEIGHT)];
textField.borderStyle = UITextBorderStyleNone;
textField.textColor = [UIColor blackColor];
textField.backgroundColor = [UIColor whiteColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.leftView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 1)] autorelease];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];

TextFieldDelegate* delegate = [[TextFieldDelegate alloc] initWithPairs:[inputbox inputOnView] and:textField];
textField.delegate = delegate;
inputbox.inputDelegate = delegate;
[textField addTarget:delegate action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

[container addSubview:textField];
((UITextField*)[inputbox inputOnView]).inputAccessoryView = container;
[inputbox setInputOnToolbar:textField];

[textField release];
}

- (id)createTextView:(const cc::EditBox::ShowInfo *)showInfo {
InputBoxPair* ret;
CGRect viewRect = UIApplication.sharedApplication.delegate.window.rootViewController.view.frame;
NSString* inputType = getTextInputType(showInfo);

if ((ret = [textInputDictionnary objectForKey:inputType])) {
[[ret inputOnView] setFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)];

UIView *accessoryView = [[ret inputOnView] inputAccessoryView];
CGRect safeRect = getSafeAreaRect();
accessoryView.frame = CGRectMake(0,
0,
safeRect.size.width,
getTextInputHeight(g_isMultiline) + ITEM_MARGIN_HEIGHT);
((UITextView*)[ret inputOnToolbar]).frame = CGRectMake(0,
0,
safeRect.size.width,
getTextInputHeight(g_isMultiline));
} else {
ret = [[InputBoxPair alloc] init];
[ret setInputOnView:[[UITextView alloc]
initWithFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)]];
[textInputDictionnary setValue:ret forKey:inputType];
[self addInputAccessoryViewForTextView:ret with:showInfo];
}

((UITextView*)[ret inputOnToolbar]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
((UITextView*)[ret inputOnView]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
return ret;
}

- (id)createTextField:(const cc::EditBox::ShowInfo*)showInfo {
InputBoxPair* ret;
CGRect viewRect = UIApplication.sharedApplication.delegate.window.rootViewController.view.frame;
NSString* inputType = getTextInputType(showInfo);

if ((ret = [textInputDictionnary objectForKey:inputType])) {
[[ret inputOnView] setFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)];

UIView *accessoryView = [[ret inputOnView] inputAccessoryView];
CGRect safeRect = getSafeAreaRect();
accessoryView.frame = CGRectMake(0,
0,
safeRect.size.width,
TEXT_LINE_HEIGHT + ITEM_MARGIN_HEIGHT);
((UITextField*)[ret inputOnToolbar]).frame = CGRectMake(0,
0,
safeRect.size.width,
TEXT_LINE_HEIGHT);
} else {
ret = [[InputBoxPair alloc] init];
[ret setInputOnView:[[UITextField alloc]
initWithFrame:CGRectMake(showInfo->x,
viewRect.size.height - showInfo->y - showInfo->height,
showInfo->width,
showInfo->height)]];
[textInputDictionnary setValue:ret forKey:inputType];
[self addInputAccessoryViewForTextField:ret with:showInfo];
}

((UITextField*)[ret inputOnToolbar]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
((UITextField*)[ret inputOnView]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
setTextFieldReturnType((UITextField*)[ret inputOnToolbar], showInfo->confirmType);
setTextFieldKeyboardType((UITextField*)[ret inputOnToolbar], showInfo->inputType);

return ret;
}

- (void)show:(const cc::EditBox::ShowInfo*)showInfo {
g_maxLength = showInfo->maxLength;
g_isMultiline = showInfo->isMultiline;

if (g_isMultiline) {
curView = [self createTextView:showInfo];
} else {
curView = [self createTextField:showInfo];
}

[[curView inputOnView] setHidden:INPUTBOX_HIDDEN];
UIView *view = UIApplication.sharedApplication.delegate.window.rootViewController.view;

[view addSubview:[curView inputOnView]];
[[curView inputOnView] becomeFirstResponder];
[[curView inputOnToolbar] becomeFirstResponder];
}

- (void)hide {
[[curView inputOnView] becomeFirstResponder];
[[curView inputOnView] removeFromSuperview];
[[curView inputOnToolbar] resignFirstResponder];
[[curView inputOnView] resignFirstResponder];
}

- (InputBoxPair*)getCurrentViewInUse {
return curView;
}

- (NSString*)getCurrentText {
if (g_isMultiline) {
return [(UITextView*)[curView inputOnToolbar] text];
}
return [(UITextField*)[curView inputOnToolbar] text];
}

@end

/*************************************************************************
Implementation of EditBox.
************************************************************************/

namespace cc {

bool EditBox::_isShown = false;

void EditBox::show(const cc::EditBox::ShowInfo &showInfo) {
[[EditboxManager sharedInstance] show:&showInfo];
EditBox::_isShown = true;
}

void EditBox::hide() {
[[EditboxManager sharedInstance] hide];
EditBox::_isShown = false;
}

bool EditBox::complete() {
if (!EditBox::_isShown) {
return true;
}
NSString *text = [[EditboxManager sharedInstance] getCurrentText];
callJSFunc("complete", [text UTF8String]);
EditBox::hide();
return true;
}

} // namespace cc

这里改完之后就行啦

+