服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

canvas里断行的方法


  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;
}

扫描关注微信公众号