自适应文字长度

This commit is contained in:
钱涛 2024-01-17 10:55:42 +08:00
parent 3f49076a19
commit a3aa0f5ca1
1 changed files with 18 additions and 10 deletions

View File

@ -3,6 +3,9 @@ package com.engine.salary.util.page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -91,18 +94,23 @@ public class SalaryPageUtil {
endIndex > source.size() ? source.size() : endIndex);
}
static Font font = new Font("Arial", Font.PLAIN, 12); // 设置字体样式大小等属性
static FontRenderContext frc = new FontRenderContext(null, true, false);
// GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
/**
* 自适应文字长度
* @param chars
* @param width
* @return
*/
public static String selfAdaption(String chars, Integer width) {
if (width != null && width != 0){
if (width != null && width != 0) {
return width + "";
}
int adaption = 0;
if (chars != null) {
adaption = chars.length() * 12 + 55;
}
if (adaption < 79) {
adaption = 79;
}
return adaption + "";
Rectangle2D bounds = font.getStringBounds(chars, frc);
int pxLength = (int) Math.ceil(bounds.getWidth());
return pxLength + 55 + "";
}
}