From 9c9d2c24bf719e40f2b7b3e80acffc55bc385fe0 Mon Sep 17 00:00:00 2001 From: Dhilan007 Date: Wed, 12 Mar 2014 21:40:58 +0800 Subject: [PATCH] EditBox:fixes clip the text width to fit to the text box on Android and IOS. --- .../GUI/CCEditBox/CCEditBoxImplAndroid.cpp | 19 +++++++++---------- .../GUI/CCEditBox/CCEditBoxImplAndroid.h | 4 ++-- extensions/GUI/CCEditBox/CCEditBoxImplIOS.h | 4 ++-- extensions/GUI/CCEditBox/CCEditBoxImplIOS.mm | 17 +++++++---------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.cpp b/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.cpp index d3c2ac4743..88900fa8a0 100644 --- a/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.cpp +++ b/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.cpp @@ -68,14 +68,16 @@ static const int CC_EDIT_BOX_PADDING = 5; bool EditBoxImplAndroid::initWithSize(const Size& size) { int fontSize = getFontSizeAccordingHeightJni(size.height-12); - _label = LabelTTF::create("", "", size.height-12); + _label = Label::create(); + _label->setFontSize(size.height-12); // align the text vertically center _label->setAnchorPoint(Point(0, 0.5f)); _label->setPosition(Point(CC_EDIT_BOX_PADDING, size.height / 2.0f)); _label->setColor(_colText); _editBox->addChild(_label); - _labelPlaceHolder = LabelTTF::create("", "", size.height-12); + _labelPlaceHolder = Label::create(); + _labelPlaceHolder->setFontSize(size.height-12); // align the text vertically center _labelPlaceHolder->setAnchorPoint(Point(0, 0.5f)); _labelPlaceHolder->setPosition(Point(CC_EDIT_BOX_PADDING, size.height / 2.0f)); @@ -178,15 +180,12 @@ void EditBoxImplAndroid::setText(const char* pText) _label->setString(strToShow.c_str()); // Clip the text width to fit to the text box - // FIXME: After re-implement LabelTTF by Label, '(g|s)etTextureRect' will not work, it's because LabelTTF is inherited from Node rather than Sprite now. - - // float fMaxWidth = _editSize.width - CC_EDIT_BOX_PADDING * 2; - // Rect clippingRect = _label->getTextureRect(); - // if(clippingRect.size.width > fMaxWidth) { - // clippingRect.size.width = fMaxWidth; - // _label->setTextureRect(clippingRect); - // } + float fMaxWidth = _editSize.width - CC_EDIT_BOX_PADDING * 2; + auto labelSize = _label->getContentSize(); + if(labelSize.width > fMaxWidth) { + _label->setDimensions(fMaxWidth,labelSize.height); + } } else { diff --git a/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.h b/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.h index 5e4c7ebf51..69a1f3e1a6 100644 --- a/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.h +++ b/extensions/GUI/CCEditBox/CCEditBoxImplAndroid.h @@ -84,8 +84,8 @@ public: virtual void closeKeyboard(); private: - LabelTTF* _label; - LabelTTF* _labelPlaceHolder; + Label* _label; + Label* _labelPlaceHolder; EditBox::InputMode _editBoxInputMode; EditBox::InputFlag _editBoxInputFlag; EditBox::KeyboardReturnType _keyboardReturnType; diff --git a/extensions/GUI/CCEditBox/CCEditBoxImplIOS.h b/extensions/GUI/CCEditBox/CCEditBoxImplIOS.h index b64208f4d2..329d30c55a 100644 --- a/extensions/GUI/CCEditBox/CCEditBoxImplIOS.h +++ b/extensions/GUI/CCEditBox/CCEditBoxImplIOS.h @@ -123,8 +123,8 @@ private: void adjustTextFieldPosition(); void placeInactiveLabels(); - LabelTTF* _label; - LabelTTF* _labelPlaceHolder; + Label* _label; + Label* _labelPlaceHolder; Size _contentSize; Point _position; Point _anchorPoint; diff --git a/extensions/GUI/CCEditBox/CCEditBoxImplIOS.mm b/extensions/GUI/CCEditBox/CCEditBoxImplIOS.mm index 13002597df..d2ba95be02 100644 --- a/extensions/GUI/CCEditBox/CCEditBoxImplIOS.mm +++ b/extensions/GUI/CCEditBox/CCEditBoxImplIOS.mm @@ -327,13 +327,13 @@ void EditBoxImplIOS::initInactiveLabels(const Size& size) { const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String]; - _label = LabelTTF::create("", "", 0.0f); + _label = Label::create(); _label->setAnchorPoint(Point(0, 0.5f)); _label->setColor(Color3B::WHITE); _label->setVisible(false); _editBox->addChild(_label, kLabelZOrder); - _labelPlaceHolder = LabelTTF::create("", "", 0.0f); + _labelPlaceHolder = Label::create(); // align the text vertically center _labelPlaceHolder->setAnchorPoint(Point(0, 0.5f)); _labelPlaceHolder->setColor(Color3B::GRAY); @@ -362,14 +362,11 @@ void EditBoxImplIOS::setInactiveText(const char* pText) _label->setString(getText()); // Clip the text width to fit to the text box - // FIXME: After re-implement LabelTTF by Label, '(g|s)etTextureRect' will not work, it's because LabelTTF is inherited from Node rather than Sprite now. - // float fMaxWidth = _editBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2; - // Rect clippingRect = _label->getTextureRect(); - // if(clippingRect.size.width > fMaxWidth) - // { - // clippingRect.size.width = fMaxWidth; - // _label->setTextureRect(clippingRect); - // } + float fMaxWidth = _editBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2; + Size labelSize = _label->getContentSize(); + if(labelSize.width > fMaxWidth) { + _label->setDimensions(fMaxWidth,labelSize.height); + } } void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)