【Problem Description】
DrawString in JavaMe Graphics class does not support text wrapping. When drawing longer strings, the text is drawn on the same line, and the strings that exceed the screen are truncated. How to make the drawn text automatically wrap?
【analyze】
drawString cannot implement automatic line wrapping, but can implement positioning of text drawing. Therefore, it is possible to consider splitting the text into multiple substrings and drawing the substrings. The splitting strategy is as follows:
1 When encountering a newline character, split it;
2 When the length of the string is greater than the set length (usually the width of the screen), split it.
【step】
1 Define a String and String [] object;
private String info; private String info_wrap[];
2 Implement the automatic string wrapping function
StringDealMethod.java
package com.token.util; import java.util.Vector; import javax.microedition.lcdui.Font; public class StringDealMethod { public StringDealMeth od() { } // String cutting, implement automatic string wrapping public static String[] format (String text, int maxWidth, Font ft) { String[] result = null; Vector tempR = new Vector(); int lines = 0; int len = text.length(); int index0 = 0; i nt index1 = 0; boolean wrap; while (true) { int widths = 0; wrap = false; for (index0 = index1; index1 < len; index1++) { if (text.charAt(index1) == '/n') { index1++; wrap = true; break; } widths = ft.charWidth(text.charAt(index1)) + widths; if (widthes > maxWidth) { break; } } lines++; if (wrap) { tempR.add Element(text.substring(index0, index1 - 1)); } else { tempR.addElement(text.substring(index0, index1)); } if (index1 >= len) { break; } } result = new String[lines]; tempR.copyInt o(result); return result; } public static String[] split(String original, String separator) { Vector nodes = new Vector(); //System.out.println("split start......... .... ......"); //Parse nodes into vector int index = original.indexOf(separator); while(index>=0) { nodes.addElement( original.substring(0, index) ); orig inal = original .substring(index+separator.length()); index = original.indexOf(separator); } // Get the last node nodes.addElement( original ); // Create splitted strin g array String[] result = new String[ nodes .size() ]; if( nodes.size()>0 ) { for(int loop=0; loop<nodes.size(); loop++) { result[loop] = (String)nodes.elementAt(loop); //System.out.println(result[loop]); } } return result; } }
3 Call the split function to realize the splitting of strings
int width = getWidth(); Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE); info = "Welcome! /n" +"1 MVC test; /n" +"2 Automatic wrapping Test, draw a string that can automatically recognize line breaks. /n"; info_wrap = StringDealMethod.format(info, width-10, ft);
4 Draw a string
int width = getWidth(); Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE); info = "Welcome! /n" +"1 MVC test; /n" +"2 Automatic wrapping Test, draw a string that can automatically recognize line breaks. /n"; info_wrap = StringDealMethod.format(info, width-10, ft);
The drawing effect is shown in Figure 1: