In games, especially in scenario games, a large number of texts are often required. To display the text on a small cell phone screen, you must process the text so that it can correctly wrap the text within the range of the width you want to display. Next I will introduce in detail how to process text line breaks.
Calculate the position where the line feed is needed. Here we use linewd, the width to be displayed, and "/N" as the line feed sign.
Static public int changline (string STR, Font font, int linewd, Boolean fullword)
{// Calculate the position where the line feed is needed. Str: the text to be displayed Font: The text font linewd: the width to be displayed
Int Len = 0, WD = 0;
For (INT I = 0; I <Str. Length (); I ++)
{
If (Str. charat (I) = '/N ')
{
If (I = 0)
Return Len + 1;
Else
Return Len + 2;
}
WD + = font. charwidth (Str. charat (I ));
If (WD> linewd)
{
If (fullword)
{
For (Int J = Len; j> = 0; j --)
{
If (Str. charat (j) <0x30 | Str. charat (j) >=128)
{
Len = J;
Break;
}
}
}
Return Len + 1;
}
Len = I;
}
Return 0;
}
After calculating the location, it starts to be a text branch.
Static public void doline (string infostr, int Len)
{// String branch for ease of Display
String tmpstr;
Vector infolines = NULL;
Infolines = new vector ();
Int tmpint; // the position where the line feed is required.
While (true)
{
Tmpint = changline (infostr, defaultfont, Len, false );
If (tmpint = 0)
{
Infolines. addelement (infostr );
Break;
}
Else
{
If (infostr. charat (tmpint-1) = '/N ')
Tmpstr = infostr. substring (0, tmpint-1 );
Else
Tmpstr = infostr. substring (0, tmpint );
Infolines. addelement (tmpstr );
Infostr = infostr. substring (tmpint, infostr. Length ());
}
}
}
The above is the code for processing text branches. Next I will introduce the scrolling Effect of text in the program. Please pay attention to subsequent articles.