由于canvas中的drawstring 函数不能自动换行,因此在使用中必须考虑到换行问题
而换行很麻烦的是中英文混合,而且英文单词不能拆开显示。因此想了办法,可能在英文单词没有空格的时候处理还有些小问题,以后继续修改~~~~如有问题尽管提出
@string : 源字符串
@font: 显示使用的字体
@FrameWidth : 屏幕宽度
public Vector dealstrings( String string ,Font font , int FrameWidth ){
int k = 0;
Vector tempvec = new Vector();
String tempstring = string;
int len = tempstring.length();
for( int j = 0 ,i = 0 ; i <= len ;i++ ){
//处理到字符串结束:
if(i >= tempstring.length())
{
tempvec.addElement(tempstring.substring(j,i));
break;
}
if (font.stringWidth(tempstring.substring(j,i)) <= FrameWidth && font.stringWidth(tempstring.substring(j,i+1)) > FrameWidth)
{
//当最后一个字符是中文,以及除空格以外的字符,符号时的处理
if( ((int)tempstring.charAt(i-1) >= 19968 && (int)tempstring.charAt(i-1) <= 171941) ||
( (int)tempstring.charAt(i-1) >= 0 && (int)tempstring.charAt(i-1) <= 64 && (int)tempstring.charAt(i-1) != 32 ) ||
( (int)tempstring.charAt(i-1) >= 91 && (int)tempstring.charAt(i-1) <= 96 ) ||
( (int)tempstring.charAt(i-1) >= 123 && (int)tempstring.charAt(i-1) <= 127 )
){
tempvec.addElement(tempstring.substring(j,i));
j=i;
continue;
}
//当最后一个字符或者下一个是空格
else { k = i;
while(tempstring.charAt(i) != ' ' && tempstring.charAt(i-1) != ' ')
{ i--;
if(i<=j){
i= k ;
break;
}
}
if(tempstring.charAt(i) == ' ' ){
tempvec.addElement(tempstring.substring(j,i));
i++;
j=i;
continue;
}
else {
tempvec.addElement(tempstring.substring(j,i));
j=i;
continue;
}
}
}
}
return tempvec;
}