I need to know the metrics of a label in advance and then clear its text to add it char by char at a latter time. This is used to achieve the effect of the attached video.
I have successfully implemented the same for other libraries and game engines. As long as there is a label or text field ui component that auto resizes to enclose all its text content it can be done.
My problem in porting this solution to cocos2d is that Label seems to strip white spaces at the end. When I set the full text and then try to use getContentSize() to measure the space requirements of that text, the result doesnāt include the last white space.
This is how Iām calculating the size of a white space, for a particular font, as a temporal fix:
Which version of cocos2d-x are you using at the moment?
Something was changed in the v3.17 release, which broke the way Label and UIRichText elements worked by trimming off the white space at the end of the line, even if the line wasnāt being split into multiple lines.
Iāve checked in a fix for it, so if you want to test it out, just merge the changes in with your version and please let me know if it works for you.
My version is 3.17. Merging. I will post again to let you know after I build and test again, but judging for what that code does, It will probably work.
Update: Confirming. After merging the changes, white spaces at the end arenāt removed any more. I deleted my temporal fix as now it causes double spacing between labels in my game.
If it gets rejected I will have to calculate spaces again to manually apply to labels position when there are many labels in the same line. So I hope it is not. The problem with doing it that way is that Iām thinking in allowing different fonts in the same line in my gameās dialog box, calculating the spaces for each loaded font would probably impose some overhead.
It seems that UIRichText.h can be reverted back to original from 3.17 release.
That file, yes, reverted back to the original in the 3.17 release, but UIRichText.cpp still required the removal of a section of code that was forcing the trimming of trailing spaces. Here is the commit with the change: