From a3aa0f5ca10a9228a548b9e16cef3ca64cfe53b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=B1=E6=B6=9B?= <15850646081@163.com> Date: Wed, 17 Jan 2024 10:55:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E9=80=82=E5=BA=94=E6=96=87=E5=AD=97?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../salary/util/page/SalaryPageUtil.java | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) 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 + ""; } }