diff --git a/src/com/engine/salary/util/page/SalaryPageUtil.java b/src/com/engine/salary/util/page/SalaryPageUtil.java index bc8892eee..a91fe1d2a 100644 --- a/src/com/engine/salary/util/page/SalaryPageUtil.java +++ b/src/com/engine/salary/util/page/SalaryPageUtil.java @@ -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 + ""; } }