首页 > 学院 > 开发设计 > 正文

Canvas里断行的方法

2019-11-18 15:58:41
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表