Skip to content

Odd behavior with label overflow setting: Label::Overflow::SHRINK #2503

Closed
@rh101

Description

@rh101
  • axmol version: 2.5
  • devices test on:
  • developing environments
    • NDK version: r23c
    • Xcode version: 14.2+
    • Visual Studio:
      • VS version: 2022 (17.9+)
      • MSVC version: 19.39+
      • Windows SDK version: 10.0.22621.0+
    • cmake version:
      Steps to Reproduce:
auto testLabel = Label::createWithTTF("COLLECTER", "fonts/arial.ttf", 38, Vec2(205, 103));
testLabel->setOverflow(Label::Overflow::SHRINK);
testLabel->setPositionNormalized(Vec2(0.5f, 0.7f));
addChild(testLabel);

Expected output:

Image

Actual output:

Image

When Label::setOverflow() is called with Label::Overflow::SHRINK, the expectation is that the text will shrink to fit on a line, and not to break words.

When it gets to this section of code, isVerticalClamp returns false, because the _textDesiredHeight is 84 and _contentSize.height is 103 (from the example code above):

if (_overflow == Overflow::SHRINK)
{
    float fontSize = this->getRenderingFontSize();

    if (fontSize > 0 && isVerticalClamp())
    {
        this->shrinkLabelToContentSize(AX_CALLBACK_0(Label::isVerticalClamp, this));
    }
}
...
...
bool Label::isVerticalClamp()
{
    if (_textDesiredHeight > _contentSize.height)
    {
        return true;
    }
    else
    {
        return false;
    }
}

This does not seem like correct behavior, because it is prioritizing breaking the words, when it should be shrinking the size of the text instead.

Also, the reason the dimensions are set as they are in the example above is because the label can be passed text that contains spaces. For example, "BUTTON ONE", so you would expect it to break at the space, and show 2 lines. In that case, it would look correct, like this:

Image

The only work-around I can think of at the moment is to manually set the button dimensions to cap them at the size of one or two (or more) lines, but that requires knowledge of the contents of the string, to know if it has spaces/line-breaks or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcocos2dxThe issue present in original cocos2dxhelp wantedExtra attention is neededpinned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions