【已解决!!!】cocos2d-x 3.10 单行输入显示区域比设置的长度小很多,想知道这是不是BUG。

部分代码如下

_editBox[0]->setMaxLength(350);
_editBox[0]->setPlaceholderFontColor(Color4B::GRAY);
_editBox[0]->setInputMode(EditBox::InputMode::SINGLE_LINE);
_editBox[0]->setInputFlag(EditBox::InputFlag::INITIAL_CAPS_SENTENCE);
_editBox[0]->setReturnType(EditBox::KeyboardReturnType::DONE);

具体如下图

蓝色框是我设置输入框的大小,红色框是我输入时,能显示的最大长度。不知道为啥会这样,不应该是显示我设置的最大输入长度吗?

单行输入时外面设置的Size最终会被 initWithSizeAndBackgroundSprite 里的Size取代。不能自动适应。

问题找到了。在editBoxEditingBegin(EditBox* editBox) 这个方法中重新设置EditBox的Size,就不出出现上图出现的问题了。

Cocos2d-x 3.10
XCode
MacOS 10.12.3

修改下面两个文件中方法:

1、 UIEditBoxImpl-ios.mm

void EditBoxImplIOS::createNativeControl(const Rect& frame)
{
// auto glview = cocos2d::Director::getInstance()->getOpenGLView();
//
// Rect rect(0, 0, frame.size.width * glview->getScaleX(), frame.size.height * glview->getScaleY());
//
// float factor = cocos2d::Director::getInstance()->getContentScaleFactor();
//
// rect.size.width /= factor;
// rect.size.height /= factor;
//
//
auto director = cocos2d::Director::getInstance();
auto glview = director->getOpenGLView();
CCEAGLView *eaglview = static_cast<CCEAGLView *>(glview->getEAGLView());
float factor = eaglview.contentScaleFactor;

Rect rect(0, 0, frame.size.width / factor, frame.size.height / factor);

_systemControl = [[UIEditBoxImplIOS_objc alloc] initWithFrame:CGRectMake(rect.origin.x,
                                                                         rect.origin.y,
                                                                         rect.size.width,
                                                                         rect.size.height)
                                                      editBox:this];

}

2、 CCUIEditBoxIOS.mm

  • (void)setContentSize:(CGSize)size
    {
    CGRect frame = self.textInput.frame;
    frame.size = size;
    self.textInput.frame = frame;
    self.frameRect = CGRectMake(0, 0, size.width, size.height);
    }