cocos2d-x-3.0


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);

Related Links

Is it possible to “pass through” a cancelled click event?
Which API should I use for cocos2dx to see my c++ crashes
cocos2dx scheduleUpdateWithPriorityLua
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

Categories

HOME
sql-server-2008
json
jsf
payment-gateway
reverse-engineering
xamarin.android
textwatcher
facebook-graph-api
deep-linking
actionscript
search-engine
value
user
ipfs
jboss7.x
cortex-a
cs-cart
viber
activesync
zoomcharts
chocolatey
distance
shippo
epicor
bootstrap-typeahead
yosys
ab-initio
pentaho-report-designer
jackson-dataformat-csv
twitter-bootstrap-2
phpspreadsheet
google-guava-cache
liquid-xml
initializer
magiczoomplus
fltk
active-model-serializers
delphi-10.1-berlin
android-preferences
ddms
ical-dotnet
datalog
crop
stress-testing
email-parsing
recycle-bin
ideone
production-environment
amazon-machine-learning
spell-checking
windows-95
heroku-postgres
adler32
hspec
chessboard.js
gemini
mongo-c-driver
scriptlet
inet
sun-codemodel
magento-1.4
license-key
test-class
function-fitting
document-oriented-db
vips
evo
kendo-menu
registrykey
visual-studio-code
mogrify
vine
isml
epl
activity-streams
utf-32
scala-2.11
dalekjs
unison
zipalign
opendata
driver-signing
ienumerator
shiva3d
gdataxml
phpsh
dynamic-data
redirectstandardoutput
die
ets
out-of-browser
carbide
hmacsha1
nt4
ajaxpro

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App