Cocos2d x 3.0 获取标签中已包装文本的大小

Cocos2d x 3.0 获取标签中已包装文本的大小,cocos2d-x-3.0,Cocos2d X 3.0,如果我在500x500区域中创建一个标签,并使用wordwrap,我如何才能找到包装文本的高度?我要找的是黄色的高度,不是鲑鱼的高度 这似乎有点违反直觉。 首先,设置高度过大的尺寸标注。 调用getLineHeight和getStringNumLines将根据传递的宽度计算高度。 将宽度和高度发送回setDimensions。 现在,标签getContentSize()将返回文本的实际大小 即 @idrise的答案不适用于系统字体,这里我给出了一个更灵活的答案 假设我们想要创建一个文本/标签,该

如果我在500x500区域中创建一个标签,并使用wordwrap,我如何才能找到包装文本的高度?我要找的是黄色的高度,不是鲑鱼的高度


这似乎有点违反直觉。 首先,设置高度过大的尺寸标注。 调用getLineHeight和getStringNumLines将根据传递的宽度计算高度。 将宽度和高度发送回setDimensions。 现在,标签getContentSize()将返回文本的实际大小


@idrise的答案不适用于系统字体,这里我给出了一个更灵活的答案

假设我们想要创建一个文本/标签,该文本/标签具有固定的宽度,但根据文本的长度具有动态的高度。为此,您可以使用以下代码:

Label*lbl=Label::createWithSystemFont(“aaa”,“Arial”,50);
lbl->设置尺寸(固定宽度,0);//“0”表示我们不关心垂直换行,因此`getContentSize().height`根据文本长度提供动态高度
////
自动动态灯光=标题->getContentSize().height;//根据文本的长度:)
显然,对于固定高度,你也可以这样做


希望对某人有所帮助:

他们添加了您想要的功能:

Added three overflow type to new label: CLAMP, SHRINK, RESIZE_HEIGHT.

Overflow type is used to control label overflow result, In SHRINK mode, the font size will change dynamically to adapt the content size. In CLAMP mode, when label content goes out of the bounding box, it will be clipped, In RESIZE_HEIGHT mode, you can only change the width of label and the height is changed automatically. For example:

//Change the label's Overflow type
label->setOverflow(Label::Overflow::RESIZE_HEIGHT);

    mTexto=Label::createWithTTF(mTextoHelp.c_str(),CCGetFont(), 30);
    mTexto->setHeight(100.f);
    mTexto->setOverflow(Label::Overflow::RESIZE_HEIGHT);
    mTexto->setDimensions(mSize.width*0.8f, 0.f);

当使用
Label::createWithSystemFont(..)
时,此答案给出了
不支持的系统字体
getLineHeight()
中断言。对于这个场景,请检查我的答案。谢谢。我正在为RichText查找类似setDimensions的内容,但找不到。一般来说,我喜欢cocos2d-xapi,但现在我觉得RichText似乎是cocos2d-x中最差的api。最终,好的旧标签起了作用。
Added three overflow type to new label: CLAMP, SHRINK, RESIZE_HEIGHT.

Overflow type is used to control label overflow result, In SHRINK mode, the font size will change dynamically to adapt the content size. In CLAMP mode, when label content goes out of the bounding box, it will be clipped, In RESIZE_HEIGHT mode, you can only change the width of label and the height is changed automatically. For example:

//Change the label's Overflow type
label->setOverflow(Label::Overflow::RESIZE_HEIGHT);

    mTexto=Label::createWithTTF(mTextoHelp.c_str(),CCGetFont(), 30);
    mTexto->setHeight(100.f);
    mTexto->setOverflow(Label::Overflow::RESIZE_HEIGHT);
    mTexto->setDimensions(mSize.width*0.8f, 0.f);