Skip to content

Commit

Permalink
Merge pull request cocos2d#14033 from m-yukio/feature/fixed-richtext
Browse files Browse the repository at this point in the history
Support the new line element. Fixed a problem of UIRichText.
  • Loading branch information
zilongshanren committed Nov 19, 2015
2 parents 27a5cdb + bc4bd99 commit 7ea90c2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
60 changes: 60 additions & 0 deletions cocos/ui/UIRichText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ bool RichElementCustomNode::init(int tag, const Color3B &color, GLubyte opacity,
return false;
}

RichElementNewLine* RichElementNewLine::create(int tag, const Color3B& color, GLubyte opacity)
{
RichElementNewLine* element = new (std::nothrow) RichElementNewLine();
if (element && element->init(tag, color, opacity))
{
element->autorelease();
return element;
}
CC_SAFE_DELETE(element);
return nullptr;
}

RichText::RichText():
_formatTextDirty(true),
_leftSpaceWidth(0.0f),
Expand Down Expand Up @@ -214,6 +226,11 @@ void RichText::formatText()
elementRenderer = elmtCustom->_customNode;
break;
}
case RichElement::Type::NEWLINE:
{
addNewLine();
break;
}
default:
break;
}
Expand Down Expand Up @@ -249,6 +266,11 @@ void RichText::formatText()
handleCustomRenderer(elmtCustom->_customNode);
break;
}
case RichElement::Type::NEWLINE:
{
addNewLine();
break;
}
default:
break;
}
Expand Down Expand Up @@ -279,6 +301,44 @@ void RichText::handleTextRenderer(const std::string& text, const std::string& fo
std::string curText = text;
size_t stringLength = StringUtils::getCharacterCountInUTF8String(text);
int leftLength = stringLength * (1.0f - overstepPercent);

// The adjustment of the new line position
auto originalLeftSpaceWidth = _leftSpaceWidth + textRendererWidth;
auto leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
auto leftWidth = textRenderer->getContentSize().width;
if (originalLeftSpaceWidth < leftWidth) {
// Have protruding
for (;;) {
leftLength--;
leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
leftWidth = textRenderer->getContentSize().width;
if (leftWidth <= originalLeftSpaceWidth) {
break;
}
else if (leftLength <= 0) {
break;
}
}
}
else if (leftWidth < originalLeftSpaceWidth) {
// A wide margin
for (;;) {
leftLength++;
leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
textRenderer->setString(leftStr);
leftWidth = textRenderer->getContentSize().width;
if (originalLeftSpaceWidth < leftWidth) {
leftLength--;
break;
}
else if (stringLength <= leftLength) {
break;
}
}
}

//The minimum cut length is 1, otherwise will cause the infinite loop.
if (0 == leftLength) leftLength = 1;
std::string leftWords = Helper::getSubStringOfUTF8String(curText,0,leftLength);
Expand Down
38 changes: 37 additions & 1 deletion cocos/ui/UIRichText.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class CC_GUI_DLL RichElement : public Ref
{
TEXT,
IMAGE,
CUSTOM
CUSTOM,
NEWLINE
};

/**
Expand Down Expand Up @@ -243,6 +244,41 @@ class CC_GUI_DLL RichElementCustomNode : public RichElement
friend class RichText;
};

/**
*@brief Rich element for new line.
*/
class CC_GUI_DLL RichElementNewLine : public RichElement
{
public:

/**
* @brief Default constructor.
* @js ctor
* @lua new
*
*/
RichElementNewLine(){_type = Type::NEWLINE;};

/**
* @brief Default destructor.
* @js NA
* @lua NA
*/
virtual ~RichElementNewLine(){};

/**
* @brief Create a RichElementNewLine with various arguments.
*
* @param tag A integer tag value.
* @param color A color in Color3B.
* @param opacity A opacity in GLubyte.
* @return A RichElementNewLine instance.
*/
static RichElementNewLine* create(int tag, const Color3B& color, GLubyte opacity);
protected:
friend class RichText;
};

/**
*@brief A container for displaying various RichElements.
* We could use it to display texts with images easily.
Expand Down

0 comments on commit 7ea90c2

Please sign in to comment.