It doesn't matter if you don't know some tips, but it will be awesome if you know it.
Recently, the legend of the turret has been played. This game is really good. Why is there such a comment? Although I develop a game, I do not really have a lot of games that I can play for more than an hour (excluding my own game, hey ). Although I have never played dota, I am a fan of the league of legends! Arrange silver segments! Beyond!
----------------
Now, we pull the Label item out again.
Create a label first.
Std: string star_str = "star is not star in the sky, it is on heart"; // first define a stringauto m_label = LabelTTF: create (star_str, "Arial ", 25); // create a labelm_label-> setPosition (Point (300,300); this-> addChild (m_label, 2 );
As shown in, labelTTF does not automatically wrap a line. What should we do if we want to implement a line feed?
When talking about how to wrap a Label, the first thing that comes to mind is to set the label size so that it can understand its own line feed when it reaches the border. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqOszO2809K70NC0 + sLro7o8YnI + CjxwcmUgY2xhc3M9 "brush: java;"> m_label-> setDimensions (Size (200,200); // set the label Size. Note that setContentSize () is not used here ();The running result is as follows:
The above method is convenient, but it still has its limitations: For example, a chat dialog box is similar. A feature of this dialog box is that it will change with the length of your text. If the Label size is left empty, then the dialog box will lose its flexibility.
Is there any cool solution? The answer is yes.
One advantage of LabelTTF is that it will wrap a line in the string where "\ n" exists.You can use this feature to write a function for automatic line feed. The reference code is as follows:
// Str is the input string, and length is the length of each line
Std: string HelloWorld: myWrap (std: string str, int length) {unsigned int beginPos = 0; // the initial position of the string std: string resultStr; // returned string std: vector
Str_vec; // create a string-type ordered container do {str_vec.push_back (str. substr (beginPos, length); // the effect of the substr function is similar to scissors. Cut the string between beginPos and length in str, put it in the container separately if (beginPos + length> str. size () {break; // when the length to be cropped exceeds the str length, exit the loop} else {beginPos + = length ;}} while (true ); for (unsigned int I = 0; I
Now we can use this method to override the label creation process once.
Std: string star_str = "star is not star in the sky, it is on heart"; // first define a stringauto m_label = LabelTTF: create (myWrap (star_str, 10), "Arial", 25); // create a labelm_label-> setPosition (Point (300,300); this-> addChild (m_label, 2 );
The result is shown in:
Well, it's the sauce.
Respect originality. for reprinting, please indicate the source: