Merge pull request #5736 from Dhilan007/develop_editbox

EditBox:fixed clip the text width to fit to the text box on Android and IOS.
This commit is contained in:
James Chen 2014-03-13 18:36:48 +08:00
commit 1595465361
4 changed files with 20 additions and 24 deletions

View File

@ -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
{

View File

@ -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;

View File

@ -123,8 +123,8 @@ private:
void adjustTextFieldPosition();
void placeInactiveLabels();
LabelTTF* _label;
LabelTTF* _labelPlaceHolder;
Label* _label;
Label* _labelPlaceHolder;
Size _contentSize;
Point _position;
Point _anchorPoint;

View File

@ -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)