Canvas里断行的方法
代码:--------------------------------------------------------------------------------
/**
* Wrap the text to lines by the specified font, text, maxWidth.
*/
public String[] format(String text, int maxWidth, int lines) {
String[] result = null;
if (lines > 64 lines < 0) {
lines = 64;
}
String[] tempR = new String[lines];
int lineIndex = 0;
if (text != null) {
int len = text.length();
int index0 = 0;
int index1 = 0;
for (; lineIndex < lines; lineIndex++) {
int widthes = 0;
for (index0 = index1; index1 < len; index1++) {
widthes += IMCanvas.textFont.stringWidth("" + text.charAt(index1));
if (widthes > maxWidth) {
break;
}
}
tempR[lineIndex] = new String(text.substring(index0, index1));
if (index1 >= len) {
lineIndex++;
break;
}
}
}
result = new String[lineIndex];
System.arraycopy(tempR, 0, result, 0, lineIndex);
return result;
}