Cocos2dx tips (3) wrap labels

Source: Internet
Author: User
Tags addchild

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"; // define a string first

Auto m_label = labelttf: Create (star_str, "Arial", 25); // create a label
M_label-> setposition (point (300,300 ));
This-> addchild (m_label, 2 );

 

Labelttf does not automatically wrap the 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., Add a line of code:

 

M_label-> setdimensions (SIZE (200,200); // you can specify 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; // initial position of the string
STD: String resultstr; // returned string


STD: vector <STD: String> str_vec; // create a string-type ordered container.
Do
{
Str_vec.push_back (Str. substr (beginpos, length); // The function is similar to scissors. Cut the string between beginpos and length in STR and 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 <str_vec.size (); ++ I)
{
Resultstr. append (str_vec.at (I). append ("\ n"); // extract the cropped delimiter string from the container one by one and add a line break after the string. Append () is similar to glue and sticks \ n to the end of the string.
}


// Resultstr. pop_back (); // Delete the last redundant \ n.


Return resultstr;
}

 

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"; // define a string first

Auto m_label = labelttf: Create (mywrap (star_str, 10), "Arial", 25); // create a label
M_label-> setposition (point (300,300 ));
This-> addchild (m_label, 2 );

 

The result is shown in:

 

Well, it's the sauce.

 

Refuse to be a waste of firewood. I want to be a schoolmaster! Cocos2dx Learning

Cocos2dx tips (3) wrap labels

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.