예제 #1
0
bool UITextFieldTest_LineWrap::init()
{
    if (UIScene::init())
    {
        Size widgetSize = _widget->getContentSize();
        
        // Add a label in which the textfield events will be displayed
        _displayValueLabel = Text::create("No Event","fonts/Marker Felt.ttf",30);
        _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1));
        _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5));
        _uiLayer->addChild(_displayValueLabel);
        
        // Add the alert
        Text *alert = Text::create("TextField line wrap","fonts/Marker Felt.ttf",30);
        alert->setColor(Color3B(159, 168, 176));
        alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075));
        _uiLayer->addChild(alert);
        
        // Create the textfield
        TextField* textField = TextField::create("input words here","fonts/Marker Felt.ttf",30);
        textField->ignoreContentAdaptWithSize(false);
        ((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
        textField->setContentSize(Size(240, 170));
        textField->setString("input words here");
        textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
        textField->setTextVerticalAlignment(TextVAlignment::CENTER);
        textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
        textField->addEventListener(CC_CALLBACK_2(UITextFieldTest_LineWrap::textFieldEvent, this));
        _uiLayer->addChild(textField);
        
        return true;
    }
    return false;
}
 void TextFieldReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textFieldOptions)
 {
     TextField* textField = static_cast<TextField*>(node);
     auto options = (TextFieldOptions*)textFieldOptions;
     
     std::string placeholder = options->placeHolder()->c_str();
     textField->setPlaceHolder(placeholder);
     
     std::string text = options->text()->c_str();
     textField->setString(text);
     
     int fontSize = options->fontSize();
     textField->setFontSize(fontSize);
     
     std::string fontName = options->fontName()->c_str();
     textField->setFontName(fontName);
     
     bool maxLengthEnabled = options->maxLengthEnabled() != 0;
     textField->setMaxLengthEnabled(maxLengthEnabled);
     
     if (maxLengthEnabled)
     {
         int maxLength = options->maxLength();
         textField->setMaxLength(maxLength);
     }
     bool passwordEnabled = options->passwordEnabled() != 0;
     textField->setPasswordEnabled(passwordEnabled);
     if (passwordEnabled)
     {
         std::string passwordStyleText = options->passwordStyleText()->c_str();
         textField->setPasswordStyleText(passwordStyleText.c_str());
     }
     
     
     bool fileExist = false;
     std::string errorFilePath = "";
     auto resourceData = options->fontResource();
     std::string path = resourceData->path()->c_str();
     if (path != "")
     {
         if (FileUtils::getInstance()->isFileExist(path))
         {
             fileExist = true;
         }
         else
         {
             errorFilePath = path;
             fileExist = false;
         }
         if (fileExist)
         {
             textField->setFontName(path);
         }
         else
         {
             auto label = Label::create();
             label->setString(__String::createWithFormat("%s missed", errorFilePath.c_str())->getCString());
             textField->addChild(label);
         }
     }
     
     auto widgetReader = WidgetReader::getInstance();
     widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
     
     textField->setUnifySizeEnabled(false);
     textField->ignoreContentAdaptWithSize(false);
     
     auto widgetOptions = options->widgetOptions();
     if (!textField->isIgnoreContentAdaptWithSize())
     {
         ((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
         Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
         textField->setContentSize(contentSize);
     }
     
     
 }