Get the size of wrapped text in Label
If I create a Label in a 500x500 area with wordwrap, how can I find out the height of the wrapped text ? I'm looking for the yellow height, not the salmon height.
Answer of #idrise doesn't work for system font And here I give a more flexible answer. Assume we want to create a text/label which has a fixed width, but dynamic height according to text's length. for that you can use below code: Label *lbl = Label::createWithSystemFont("aaa aaa aaa aaa aaa aaa", "Arial", 50); lbl->setDimensions(FIXED_WIDTH, 0); // "0" means we don't care about wrapping vertically, hence `getContentSize().height` give a dynamic height according to text's length //// auto dynamicHeight = title->getContentSize().height; // According to text's length :) And obviously for fixed height you can do similarly. Hope Help someone :]
This may seem a little counter intuitive. First you set the dimensions with an excessively large height. Calling getLineHeight and getStringNumLines will calculate the height based on the width passed. You send the width and height back to setDimensions. Now your labels getContentSize() will return the actual size of the text. IE label->setDimensions(width, 2000); label->setDimensions(width,label->getStringNumLines() * ceil(label->getLineHeight()));
They added the functionality you want: 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);
Is it possible to “pass through” a cancelled click event?
Which API should I use for cocos2dx to see my c++ crashes
Click issue of CCMenuItemToggle
Sometimes when I send request to fetch player image from server using httprequest
Play animations from Cocos Studio in sequence in cocos2d-js
how to use cocos2d-x 3.x expansion apk
Photon Multiplayer, Both players create their own rooms
How do I implement a 2d skeleton in cocos2d-x v3?
MenuItemLabel callback not working, never calls the callback, but the MenuItemImage works
How to use DrawNode with RenderTexture cocos2d-x
cocos2dx: Sprite3D rotating, culling error
moving back ground using single image in cocos2dx v3
cocos2d::Menu click detection is a bit off to bottom left whenever using a custom window size
How to get Button position and size after “setRotation3D” in cocos2dx 3.3
Use Spine in Cocos2dx