Skip to content

Commit 4c0361e

Browse files
kapladminggo
authored andcommitted
Fix cocos2d::EditBox padding (#18572)
1 parent 45992aa commit 4c0361e

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

cocos/ui/UIEditBox/UIEditBoxImpl-common.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ void EditBoxImplCommon::setInactiveText(const char* pText)
144144
_label->setString(pText);
145145
}
146146
// Clip the text width to fit to the text box
147-
float fMaxWidth = _editBox->getContentSize().width;
148-
float fMaxHeight = _editBox->getContentSize().height;
147+
const auto& editBoxSize = _editBox->getContentSize();
148+
const auto maxSize = applyPadding(cocos2d::Size(editBoxSize.width, editBoxSize.height));
149149
Size labelSize = _label->getContentSize();
150-
if(labelSize.width > fMaxWidth || labelSize.height > fMaxHeight)
150+
if(labelSize.width > maxSize.width || labelSize.height > maxSize.height)
151151
{
152-
_label->setDimensions(fMaxWidth, fMaxHeight);
152+
_label->setDimensions(maxSize.width, maxSize.height);
153153
}
154154
}
155155

@@ -201,7 +201,7 @@ void EditBoxImplCommon::setInputMode(EditBox::InputMode inputMode)
201201
{
202202
_editBoxInputMode = inputMode;
203203
this->setNativeInputMode(inputMode);
204-
this->placeInactiveLabels(_editBox->getContentSize());
204+
this->placeInactiveLabels(applyPadding(_editBox->getContentSize()));
205205
}
206206

207207
void EditBoxImplCommon::setMaxLength(int maxLength)
@@ -282,9 +282,9 @@ void EditBoxImplCommon::setVisible(bool visible)
282282

283283
void EditBoxImplCommon::setContentSize(const Size& size)
284284
{
285-
_contentSize = size;
286-
CCLOG("[Edit text] content size = (%f, %f)", size.width, size.height);
287-
placeInactiveLabels(size);
285+
_contentSize = applyPadding(size);
286+
CCLOG("[Edit text] content size = (%f, %f)", _contentSize.width, _contentSize.height);
287+
placeInactiveLabels(_contentSize);
288288
}
289289

290290
void EditBoxImplCommon::draw(Renderer* /*renderer*/, const Mat4& /*transform*/, uint32_t flags)
@@ -413,6 +413,11 @@ void EditBoxImplCommon::editBoxEditingChanged(const std::string& text)
413413

414414
}
415415

416+
Size ui::EditBoxImplCommon::applyPadding(const Size& sizeToCorrect) const {
417+
constexpr auto paddingLeftRight = CC_EDIT_BOX_PADDING * 2;
418+
return Size(sizeToCorrect.width - paddingLeftRight, sizeToCorrect.height);
419+
}
420+
416421
NS_CC_END
417422

418423

cocos/ui/UIEditBox/UIEditBoxImpl-common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class CC_GUI_DLL EditBoxImplCommon : public EditBoxImpl
136136
void placeInactiveLabels(const Size& size);
137137
virtual void doAnimationWhenKeyboardMove(float duration, float distance)override {};
138138

139+
Size applyPadding(const Size& size) const;
140+
139141
Label* _label;
140142
Label* _labelPlaceHolder;
141143
EditBox::InputMode _editBoxInputMode;

0 commit comments

Comments
 (0)