2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 James Chen
|
|
|
|
Copyright (c) 2013-2015 zilongshanren
|
|
|
|
Copyright (c) 2015 Mazyad Alabduljaleel
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#import "ui/UIEditBox/iOS/CCUIEditBoxIOS.h"
|
|
|
|
#import "ui/UIEditBox/iOS/CCUISingleLineTextField.h"
|
|
|
|
#import "ui/UIEditBox/iOS/CCUIMultilineTextField.h"
|
|
|
|
|
|
|
|
#import "platform/ios/CCEAGLView-ios.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
#define getEditBoxImplIOS() ((cocos2d::ui::EditBoxImplIOS*)_editBox)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
@implementation UIEditBoxImplIOS_objc
|
|
|
|
|
|
|
|
#pragma mark - Static methods
|
|
|
|
|
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
[super initialize];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
LoadUITextViewCCUITextInputCategory();
|
|
|
|
LoadUITextFieldCCUITextInputCategory();
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Init & Dealloc
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frameRect editBox:(void*)editBox
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
self = [super init];
|
2021-12-31 12:12:40 +08:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
|
|
|
|
_editState = NO;
|
|
|
|
self.frameRect = frameRect;
|
|
|
|
self.editBox = editBox;
|
|
|
|
self.dataInputMode = cocos2d::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS;
|
2019-11-23 20:27:39 +08:00
|
|
|
self.keyboardReturnType = cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[self createMultiLineTextField];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
// custom setter cleanup
|
|
|
|
self.textInput = nil;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Properties
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setTextInput:(UIView<UITextInput, CCUITextInput>*)textInput
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (_textInput == textInput)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// common init
|
|
|
|
textInput.backgroundColor = [UIColor clearColor];
|
2021-12-31 12:12:40 +08:00
|
|
|
textInput.hidden = true;
|
|
|
|
textInput.returnKeyType = UIReturnKeyDefault;
|
2019-11-23 20:27:39 +08:00
|
|
|
[textInput ccui_setDelegate:self];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Migrate properties
|
2021-12-31 12:12:40 +08:00
|
|
|
textInput.ccui_textColor = _textInput.ccui_textColor ?: [UIColor whiteColor];
|
|
|
|
textInput.ccui_text = _textInput.ccui_text ?: @"";
|
2019-11-23 20:27:39 +08:00
|
|
|
textInput.ccui_placeholder = _textInput.ccui_placeholder ?: @"";
|
2021-12-31 12:12:40 +08:00
|
|
|
textInput.ccui_font = _textInput.ccui_font ?: [UIFont systemFontOfSize:self.frameRect.size.height * 2 / 3];
|
|
|
|
textInput.ccui_placeholderFont = _textInput.ccui_placeholderFont ?: textInput.ccui_font;
|
2019-11-23 20:27:39 +08:00
|
|
|
textInput.ccui_placeholderTextColor = _textInput.ccui_placeholderTextColor ?: [UIColor lightGrayColor];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[_textInput resignFirstResponder];
|
|
|
|
[_textInput removeFromSuperview];
|
|
|
|
[_textInput release];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_textInput = [textInput retain];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[self setInputFlag:self.dataInputMode];
|
|
|
|
[self setReturnType:self.keyboardReturnType];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public methods
|
|
|
|
|
|
|
|
- (void)createSingleLineTextField
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CCUISingleLineTextField* textField = [[[CCUISingleLineTextField alloc] initWithFrame:self.frameRect] autorelease];
|
2019-11-23 20:27:39 +08:00
|
|
|
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
|
2021-12-31 12:12:40 +08:00
|
|
|
textField.borderStyle = UITextBorderStyleNone;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
self.textInput = textField;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)createMultiLineTextField
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
CCUIMultilineTextField* textView = [[[CCUIMultilineTextField alloc] initWithFrame:self.frameRect] autorelease];
|
|
|
|
self.textInput = textView;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public methods
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setFont:(UIFont*)font
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
self.textInput.ccui_font = font;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTextColor:(UIColor*)color
|
|
|
|
{
|
|
|
|
self.textInput.ccui_textColor = color;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setPlaceholderFont:(UIFont*)font
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
self.textInput.ccui_placeholderFont = font;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setPlaceholderTextColor:(UIColor*)color
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
self.textInput.ccui_placeholderTextColor = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
// multiline input
|
|
|
|
if (inputMode == cocos2d::ui::EditBox::InputMode::ANY)
|
|
|
|
{
|
|
|
|
if (![self.textInput isKindOfClass:[UITextView class]])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self createMultiLineTextField];
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (![self.textInput isKindOfClass:[UITextField class]])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self createSingleLineTextField];
|
|
|
|
}
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
switch (inputMode)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case cocos2d::ui::EditBox::InputMode::EMAIL_ADDRESS:
|
|
|
|
self.keyboardType = UIKeyboardTypeEmailAddress;
|
|
|
|
break;
|
|
|
|
case cocos2d::ui::EditBox::InputMode::NUMERIC:
|
|
|
|
self.keyboardType = UIKeyboardTypeDecimalPad;
|
|
|
|
break;
|
|
|
|
case cocos2d::ui::EditBox::InputMode::PHONE_NUMBER:
|
|
|
|
self.keyboardType = UIKeyboardTypePhonePad;
|
|
|
|
break;
|
|
|
|
case cocos2d::ui::EditBox::InputMode::URL:
|
|
|
|
self.keyboardType = UIKeyboardTypeURL;
|
|
|
|
break;
|
|
|
|
case cocos2d::ui::EditBox::InputMode::DECIMAL:
|
|
|
|
self.keyboardType = UIKeyboardTypeDecimalPad;
|
|
|
|
break;
|
|
|
|
case cocos2d::ui::EditBox::InputMode::SINGLE_LINE:
|
|
|
|
self.keyboardType = UIKeyboardTypeDefault;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self.keyboardType = UIKeyboardTypeDefault;
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setKeyboardType:(UIKeyboardType)type
|
|
|
|
{
|
|
|
|
self.textInput.keyboardType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setInputFlag:(cocos2d::ui::EditBox::InputFlag)flag
|
|
|
|
{
|
|
|
|
self.dataInputMode = flag;
|
|
|
|
switch (flag)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case cocos2d::ui::EditBox::InputFlag::PASSWORD:
|
|
|
|
// textView can't be used for input password
|
|
|
|
self.textInput.ccui_secureTextEntry = YES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_WORD:
|
|
|
|
self.textInput.autocapitalizationType = UITextAutocapitalizationTypeWords;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
|
|
|
|
self.textInput.autocapitalizationType = UITextAutocapitalizationTypeSentences;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS:
|
|
|
|
self.textInput.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::InputFlag::SENSITIVE:
|
|
|
|
self.textInput.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::InputFlag::LOWERCASE_ALL_CHARACTERS:
|
|
|
|
self.textInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setReturnType:(cocos2d::ui::EditBox::KeyboardReturnType)returnType
|
|
|
|
{
|
|
|
|
self.keyboardReturnType = returnType;
|
2021-12-31 12:12:40 +08:00
|
|
|
switch (returnType)
|
|
|
|
{
|
|
|
|
case cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeyDefault;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::KeyboardReturnType::DONE:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeyDone;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::KeyboardReturnType::SEND:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeySend;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::KeyboardReturnType::SEARCH:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeySearch;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::KeyboardReturnType::GO:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeyGo;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case cocos2d::ui::EditBox::KeyboardReturnType::NEXT:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeyNext;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
self.textInput.returnKeyType = UIReturnKeyDefault;
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTextHorizontalAlignment:(cocos2d::TextHAlignment)alignment
|
|
|
|
{
|
|
|
|
self.textInput.ccui_alignment = static_cast<NSTextAlignment>(alignment);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setText:(NSString*)text
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
self.textInput.ccui_text = text;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (NSString*)text
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return self.textInput.ccui_text ?: @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVisible:(BOOL)visible
|
|
|
|
{
|
|
|
|
self.textInput.hidden = !visible;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (NSString*)getDefaultFontName
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return self.textInput.ccui_font.fontName ?: @"";
|
|
|
|
}
|
|
|
|
|
|
|
|
- (cocos2d::ui::EditBoxDelegate::EditBoxEndAction)getEndAction
|
|
|
|
{
|
|
|
|
cocos2d::ui::EditBoxDelegate::EditBoxEndAction action = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::UNKNOWN;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (self.returnPressed)
|
|
|
|
{
|
|
|
|
if (self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::NEXT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
action = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::TAB_TO_NEXT;
|
2021-12-31 12:12:40 +08:00
|
|
|
}
|
|
|
|
else if (self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::GO ||
|
|
|
|
self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::SEND ||
|
|
|
|
self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::SEARCH)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
action = cocos2d::ui::EditBoxDelegate::EditBoxEndAction::RETURN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)setPlaceHolder:(NSString*)text
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
self.textInput.ccui_placeholder = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doAnimationWhenKeyboardMoveWithDuration:(float)duration distance:(float)distance
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView* eaglview = (CCEAGLView*)view->getEAGLView();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[eaglview doAnimationWhenKeyboardMoveWithDuration:duration distance:distance];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateFrame:(CGRect)rect
|
|
|
|
{
|
|
|
|
CGRect frame = self.textInput.frame;
|
|
|
|
frame.origin = rect.origin;
|
2021-12-31 12:12:40 +08:00
|
|
|
frame.size = rect.size;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
self.textInput.frame = frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)openKeyboard
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView* eaglview = (CCEAGLView*)view->getEAGLView();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[eaglview addSubview:self.textInput];
|
|
|
|
[self.textInput becomeFirstResponder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)closeKeyboard
|
|
|
|
{
|
|
|
|
[self.textInput resignFirstResponder];
|
|
|
|
[self.textInput removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField*)sender
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (sender == self.textInput)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
self.returnPressed = YES;
|
|
|
|
[sender resignFirstResponder];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)animationSelector
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView* eaglview = (CCEAGLView*)view->getEAGLView();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
[eaglview doAnimationWhenAnotherEditBeClicked];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITextView delegate methods
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textViewShouldBeginEditing:(UITextView*)textView
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldBeginEditing...");
|
2021-12-31 12:12:40 +08:00
|
|
|
_editState = YES;
|
2019-11-23 20:27:39 +08:00
|
|
|
_returnPressed = NO;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView* eaglview = (CCEAGLView*)view->getEAGLView();
|
|
|
|
|
|
|
|
if ([eaglview isKeyboardShown])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
getEditBoxImplIOS()->editBoxEditingDidBegin();
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textViewShouldEndEditing:(UITextView*)textView
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldEndEditing...");
|
|
|
|
_editState = NO;
|
|
|
|
getEditBoxImplIOS()->refreshInactiveText();
|
|
|
|
|
|
|
|
const char* inputText = [textView.text UTF8String];
|
|
|
|
getEditBoxImplIOS()->editBoxEditingDidEnd(inputText, [self getEndAction]);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (self.keyboardReturnType == cocos2d::ui::EditBox::KeyboardReturnType::DONE && [text isEqualToString:@"\n"])
|
2019-11-18 10:15:20 +08:00
|
|
|
{
|
|
|
|
[self closeKeyboard];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int maxLength = getEditBoxImplIOS()->getMaxLength();
|
|
|
|
if (maxLength < 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// Prevent crashing undo bug
|
|
|
|
// http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield
|
|
|
|
if (range.length + range.location > textView.text.length)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return NO;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
NSUInteger oldLength = textView.text.length;
|
2019-11-23 20:27:39 +08:00
|
|
|
NSUInteger replacementLength = text.length;
|
2021-12-31 12:12:40 +08:00
|
|
|
NSUInteger rangeLength = range.length;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NSUInteger newLength = oldLength - rangeLength + replacementLength;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return newLength <= maxLength;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)textViewDidChange:(UITextView*)textView
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int maxLength = getEditBoxImplIOS()->getMaxLength();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (textView.markedTextRange == nil)
|
|
|
|
{
|
|
|
|
if (textView.text.length > maxLength)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
textView.text = [textView.text substringToIndex:maxLength];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* inputText = [textView.text UTF8String];
|
|
|
|
getEditBoxImplIOS()->editBoxEditingChanged(inputText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UITextField delegate methods
|
|
|
|
/**
|
|
|
|
* Called each time when the text field's text has changed.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
- (void)textChanged:(UITextField*)textField
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int maxLength = getEditBoxImplIOS()->getMaxLength();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (textField.markedTextRange == nil)
|
|
|
|
{
|
|
|
|
if (textField.text.length > maxLength)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
textField.text = [textField.text substringToIndex:maxLength];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* inputText = [textField.text UTF8String];
|
|
|
|
getEditBoxImplIOS()->editBoxEditingChanged(inputText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textFieldShouldBeginEditing:(UITextField*)sender // return NO to disallow editing.
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldBeginEditing...");
|
2021-12-31 12:12:40 +08:00
|
|
|
_editState = YES;
|
2019-11-23 20:27:39 +08:00
|
|
|
_returnPressed = NO;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
auto view = cocos2d::Director::getInstance()->getOpenGLView();
|
|
|
|
CCEAGLView* eaglview = (CCEAGLView*)view->getEAGLView();
|
|
|
|
|
|
|
|
if ([eaglview isKeyboardShown])
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
[self performSelector:@selector(animationSelector) withObject:nil afterDelay:0.0f];
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
getEditBoxImplIOS()->editBoxEditingDidBegin();
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textFieldShouldEndEditing:(UITextField*)sender
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("textFieldShouldEndEditing...");
|
2021-12-31 12:12:40 +08:00
|
|
|
_editState = NO;
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* inputText = [sender.text UTF8String];
|
|
|
|
|
|
|
|
getEditBoxImplIOS()->editBoxEditingDidEnd(inputText, [self getEndAction]);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate method called before the text has been changed.
|
|
|
|
* @param textField The text field containing the text.
|
|
|
|
* @param range The range of characters to be replaced.
|
|
|
|
* @param string The replacement string.
|
|
|
|
* @return YES if the specified text range should be replaced; otherwise, NO to keep the old text.
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
- (BOOL)textField:(UITextField*)textField
|
|
|
|
shouldChangeCharactersInRange:(NSRange)range
|
|
|
|
replacementString:(NSString*)string
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int maxLength = getEditBoxImplIOS()->getMaxLength();
|
2021-12-31 12:12:40 +08:00
|
|
|
if (maxLength < 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return YES;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
// Prevent crashing undo bug
|
|
|
|
// http://stackoverflow.com/questions/433337/set-the-maximum-character-length-of-a-uitextfield
|
|
|
|
if (range.length + range.location > textField.text.length)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return NO;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
|
|
|
NSUInteger oldLength = textField.text.length;
|
2019-11-23 20:27:39 +08:00
|
|
|
NSUInteger replacementLength = string.length;
|
2021-12-31 12:12:40 +08:00
|
|
|
NSUInteger rangeLength = range.length;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
NSUInteger newLength = oldLength - rangeLength + replacementLength;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return newLength <= maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|