void TextField::setString(const std::string& text) { std::string strText(text); if (isMaxLengthEnabled()) { int max = _textFieldRenderer->getMaxLength(); long text_count = StringUtils::getCharacterCountInUTF8String(text); if (text_count > max) { strText = Helper::getSubStringOfUTF8String(strText, 0, max); } } if (isPasswordEnabled()) { _textFieldRenderer->setPasswordText(strText); _textFieldRenderer->setString(""); _textFieldRenderer->insertText(strText.c_str(), strText.size()); } else { _textFieldRenderer->setString(strText); } _textFieldRendererAdaptDirty = true; updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize()); }
void TextField::setText(const std::string& text) { std::string strText(text); if (isMaxLengthEnabled()) { int max = _textFieldRenderer->getMaxLength(); int text_count = _calcCharCount(text.c_str()); int total = text_count + _calcCharCount(getStringValue().c_str()); if (total > max) { int ascii = 0; int unicode = 0; int end = 0; int count = 0; for (int i = 0; i < total * 3; ++i) { char value = text[i]; if (value >= 0 && value <= 127) // ascii { ascii++; count++; } else { unicode++; if (unicode % 3 == 0) { count++; } } if (count == max) { break; } } end = ascii + unicode; strText = strText.substr(0, end); } } const char* content = strText.c_str(); if (isPasswordEnabled()) { _textFieldRenderer->setPasswordText(content); _textFieldRenderer->setString(""); _textFieldRenderer->insertText(content, strlen(content)); } else { _textFieldRenderer->setString(content); } _textFieldRendererAdaptDirty = true; updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize()); }
void TextField::setText(const std::string& text) { std::string strText(text); if (isMaxLengthEnabled()) { strText = strText.substr(0, getMaxLength()); } const char* content = strText.c_str(); if (isPasswordEnabled()) { _textFieldRenderer->setPasswordText(content); _textFieldRenderer->insertText(content, static_cast<int>(strlen(content))); } else { _textFieldRenderer->setString(content); } textfieldRendererScaleChangedWithSize(); }