import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
public class NewLineString {
/**
* 将字符串改为每length个像素添加一个换行
* @param s 输入字符
* @param limitWidth 限制长度
* @return
*/
public static String StringLine(String s,int limitWidth){
s = s.replaceAll("\\s*", "");
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
Font font = new Font("楷体", Font.PLAIN, 12);
StringBuilder builder = new StringBuilder();
int tmp = 0;
for (int i = 1; i < s.length(); i++)
{
if ((int)(font.getStringBounds(s.substring(tmp, i + 1), frc).getWidth()) > limitWidth)
{
builder.append(s.substring(tmp, i));
builder.append("\n");
tmp = i;
}
}
builder.append(s.substring(tmp));
return builder.toString();
}
}
测试数据
@Test
public void test() throws IOException {
String ss = "帝\n高阳之苗裔兮,\n 朕皇考曰\n伯庸。\n" +
"摄提贞于\n孟陬兮 \n ,惟庚寅吾\n以降。\n" +
"皇览揆\n余初度兮 \n ,肇锡余以\n嘉名:\n" +
"名余\n曰正则兮,字余曰\n灵均。" ;
System.out.println(StringLine(ss, 180));
}
测试结果