Cocos2dx tips (3) wrap labels

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.