Orchestration display of mixed strings in canvas

Source: Internet
Author: User
A mixed string is a string consisting of Chinese characters, English letters, various punctuation marks, and escape characters. We often choose to use CANVAS instead of advanced interfaces such as FORM, because we need to control the UI style by ourselves in the j2's program. However, in the canvas, the display of long strings requires us to cut the display according to the display width. Processing mixed strings is complicated.

First, we need to solve the number of characters to be drawn in each line. The width of punctuation marks varies according to Chinese characters and English characters. If we fix the number of characters displayed in each line, if both Chinese and English characters are displayed together, the results will show that the length difference shown in each line is serious, the display effect is not very beautiful. To solve this problem, we determine the minimum number of characters drawn in each line, because the Chinese word width is the longest, therefore, we can use the display width divided by the number of Chinese characters to get the minimum number of characters in each line. Then we try to add the next character after obtaining the minimum number of characters and then compare it with the width, if it is greater than the width, we will not add this character. If it is less than the width, we will continue to add the character until it is greater than the display width, so that the maximum number of characters can be put down in each line, make sure that each line is full.

Next, we will deal with the issue of escape characters. The Escape Character mainly processes/n escape characters. The processing of/N is mainly in two cases. One is to find/N in each row, so that we can stop the interception of the current row and access the next row, in this way, the goal of line feed is achieved. Another case is that after we just cut a line of strings, the next one is/n. In this case, we need to discard/N without processing.

These two functions are implemented in a method. The Code is as follows:

/***//**
* Cut the obtained string into a string array suitable for display.
* @ Param text refers to the string to be cut.
* @ Param font: font
* @ Param width: The displayed wide read
* @ Return refers to the string array after splitting.
*/
Public String [] getSubInfo (String text, Font font, int width )...{

Boolean isFind = false;
// Maximum character width
Int max_width = font. charWidth ('my ');
// The minimum number of characters in each row
Int char_num = width/max_width;

Vector vector = new Vector ();

While (text. Length ()> 0 )...{
Int Index = text. indexof ('');
Int start_num = 0;
String Info;
// The remaining strings are smaller than the number of characters in each line.
If (text. Length () <char_num )...{
If (index! =-1 & index <text. Length ())...{
Info = text. substring (start_num, index + 1 );
TEXT = text. substring (index + 1 );
Vector. addelement (Info );
Continue;
}
Info = text. substring (start_num );
Vector. addelement (Info );
Break;
}
If (index! =-1 & index <char_num )...{
Info = text. substring (start_num,
Start_num = (start_num + index + 1 ));
Text = text. substring (start_num );
Vector. addElement (info );
Continue;
}

Info = text. substring (start_num, start_num = (start_num + char_num ));
// Try to increase the number of characters
While (font. stringWidth (info + text. substring (start_num,
Start_num + 1) <width )...{
If (index = (start_num + 1 ))...{
Start_num + = 1;
IsFind = true;
Break;
}
Info + = text. substring (start_num, start_num = (start_num + 1 ));
If (info. length ()> = text. length ())...{
Break;
}
}
If (! IsFind & (start_num + 1) = index )...{
Start_num + = 1;
}
Text = text. substring (start_num );
Vector. addElement (info );
}
String [] text_info = new String [vector. size ()];
Vector. copyInto (text_info );
Return text_info;
}

For other escape characters, such as/T, we can add the detection and convert it.

The processing of English words at the end of the line is not implemented yet.

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.