Merge pull request #15112 from zilongshanren/improve-editbox-mac

improve mac editbox
This commit is contained in:
zilongshanren 2016-02-25 11:44:52 +08:00
commit 8f05f33a01
2 changed files with 14 additions and 19 deletions

View File

@ -48,7 +48,6 @@
self.keyboardReturnType = cocos2d::ui::EditBox::KeyboardReturnType::DEFAULT;
[self createMultiLineTextField];
[self.window.contentView addSubview:self.textInput];
}
return self;
@ -96,7 +95,8 @@
[_textInput performSelector:@selector(setBackgroundColor:) withObject:[NSColor clearColor]];
if (![_textInput isKindOfClass:[NSTextView class]]) {
[_textInput performSelector:@selector(setBordered:) withObject:[NSNumber numberWithBool:NO]];
[_textInput performSelector:@selector(setBordered:)
withObject:[NSNumber numberWithBool:NO]];
}
_textInput.hidden = NO;
_textInput.wantsLayer = YES;
@ -116,7 +116,6 @@
frame.size.width = rect.size.width;
self.textInput.frame = frame;
[self.window makeFirstResponder:self.window.contentView];
}
- (void)dealloc
@ -140,6 +139,11 @@
}else {
[self.window makeFirstResponder:self.textInput];
}
auto editbox = getEditBoxImplMac()->getEditBox();
auto oldPos = editbox->getPosition();
editbox->setPosition(oldPos + cocos2d::Vec2(10,20));
editbox->setPosition(oldPos);
}
- (void)closeKeyboard
@ -189,30 +193,18 @@
return self.textInput.ccui_font.fontName ?: @"";
}
- (void) hackingCodeForRefreshPosition
{
[self.window.contentView addSubview:self.textInput];
if (![self.textInput isKindOfClass:[NSTextView class]]) {
[self.textInput becomeFirstResponder];
}else {
[self.window makeFirstResponder:self.textInput];
}
}
- (void)setInputMode:(cocos2d::ui::EditBox::InputMode)inputMode
{
//multiline input
if (inputMode == cocos2d::ui::EditBox::InputMode::ANY) {
if (![self.textInput isKindOfClass:[NSTextView class]]) {
[self createMultiLineTextField];
[self hackingCodeForRefreshPosition];
}
}
else {
if (self.dataInputMode != cocos2d::ui::EditBox::InputFlag::PASSWORD) {
if (![self.textInput isKindOfClass:[NSTextField class]]) {
[self createSingleLineTextField];
[self hackingCodeForRefreshPosition];
}
}
}

View File

@ -63,12 +63,13 @@ bool UIEditBoxTest::init()
_editName->setPlaceHolder("Name:");
_editName->setPlaceholderFontColor(Color3B::WHITE);
_editName->setMaxLength(8);
_editName->setFontSize(editBoxSize.height/2);
_editName->setFontSize(editBoxSize.height/2);
_editName->setText("v👐👊💝");
_editName->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
_editName->setDelegate(this);
_editName->setVisible(true);
addChild(_editName);
Button* button = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
auto buttonSize = button->getContentSize();
@ -87,10 +88,11 @@ bool UIEditBoxTest::init()
_editPassword->setMaxLength(6);
_editPassword->setInputFlag(ui::EditBox::InputFlag::PASSWORD);
_editPassword->setInputMode(ui::EditBox::InputMode::SINGLE_LINE);
_editPassword->setFontSize(editBoxSize.height/2);
_editPassword->setFontSize(editBoxSize.height/2);
_editPassword->setDelegate(this);
_editPassword->setVisible(true);
addChild(_editPassword);
auto buttonPassword = (ui::Button*)button->clone();
buttonPassword->setTitleText("Multiline");
buttonPassword->setPosition(_editPassword->getPosition() + Vec2(editBoxSize.width/2 + buttonSize.width/2, 0 ));
@ -111,6 +113,7 @@ bool UIEditBoxTest::init()
_editEmail->setPlaceHolder("Email:");
_editEmail->setInputMode(ui::EditBox::InputMode::EMAIL_ADDRESS);
_editEmail->setDelegate(this);
_editEmail->setVisible(true);
_editEmailParent->addChild(_editEmail);
//It is required to use setFontSize and setContentSize after adding it to the hierarchy, so that native EditBox get the right size
_editEmail->setFontSize(30);