EditBox:fixes clip the text width to fit to the text box on Android and IOS.

This commit is contained in:
Dhilan007 2014-03-12 21:40:58 +08:00
parent e1afa0ad5c
commit 9c9d2c24bf
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) bool EditBoxImplAndroid::initWithSize(const Size& size)
{ {
int fontSize = getFontSizeAccordingHeightJni(size.height-12); 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 // align the text vertically center
_label->setAnchorPoint(Point(0, 0.5f)); _label->setAnchorPoint(Point(0, 0.5f));
_label->setPosition(Point(CC_EDIT_BOX_PADDING, size.height / 2.0f)); _label->setPosition(Point(CC_EDIT_BOX_PADDING, size.height / 2.0f));
_label->setColor(_colText); _label->setColor(_colText);
_editBox->addChild(_label); _editBox->addChild(_label);
_labelPlaceHolder = LabelTTF::create("", "", size.height-12); _labelPlaceHolder = Label::create();
_labelPlaceHolder->setFontSize(size.height-12);
// align the text vertically center // align the text vertically center
_labelPlaceHolder->setAnchorPoint(Point(0, 0.5f)); _labelPlaceHolder->setAnchorPoint(Point(0, 0.5f));
_labelPlaceHolder->setPosition(Point(CC_EDIT_BOX_PADDING, size.height / 2.0f)); _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()); _label->setString(strToShow.c_str());
// Clip the text width to fit to the text box // 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 else
{ {

View File

@ -84,8 +84,8 @@ public:
virtual void closeKeyboard(); virtual void closeKeyboard();
private: private:
LabelTTF* _label; Label* _label;
LabelTTF* _labelPlaceHolder; Label* _labelPlaceHolder;
EditBox::InputMode _editBoxInputMode; EditBox::InputMode _editBoxInputMode;
EditBox::InputFlag _editBoxInputFlag; EditBox::InputFlag _editBoxInputFlag;
EditBox::KeyboardReturnType _keyboardReturnType; EditBox::KeyboardReturnType _keyboardReturnType;

View File

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

View File

@ -327,13 +327,13 @@ void EditBoxImplIOS::initInactiveLabels(const Size& size)
{ {
const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String]; const char* pDefaultFontName = [[_systemControl.textField.font fontName] UTF8String];
_label = LabelTTF::create("", "", 0.0f); _label = Label::create();
_label->setAnchorPoint(Point(0, 0.5f)); _label->setAnchorPoint(Point(0, 0.5f));
_label->setColor(Color3B::WHITE); _label->setColor(Color3B::WHITE);
_label->setVisible(false); _label->setVisible(false);
_editBox->addChild(_label, kLabelZOrder); _editBox->addChild(_label, kLabelZOrder);
_labelPlaceHolder = LabelTTF::create("", "", 0.0f); _labelPlaceHolder = Label::create();
// align the text vertically center // align the text vertically center
_labelPlaceHolder->setAnchorPoint(Point(0, 0.5f)); _labelPlaceHolder->setAnchorPoint(Point(0, 0.5f));
_labelPlaceHolder->setColor(Color3B::GRAY); _labelPlaceHolder->setColor(Color3B::GRAY);
@ -362,14 +362,11 @@ void EditBoxImplIOS::setInactiveText(const char* pText)
_label->setString(getText()); _label->setString(getText());
// Clip the text width to fit to the text box // 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;
// float fMaxWidth = _editBox->getContentSize().width - CC_EDIT_BOX_PADDING * 2; Size labelSize = _label->getContentSize();
// Rect clippingRect = _label->getTextureRect(); if(labelSize.width > fMaxWidth) {
// if(clippingRect.size.width > fMaxWidth) _label->setDimensions(fMaxWidth,labelSize.height);
// { }
// clippingRect.size.width = fMaxWidth;
// _label->setTextureRect(clippingRect);
// }
} }
void EditBoxImplIOS::setFont(const char* pFontName, int fontSize) void EditBoxImplIOS::setFont(const char* pFontName, int fontSize)