xyc
2024-10-28 71f70458719aa6ce52d9ffd21eb5cd9d92a55fd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.zt.life.modules.mainPart.utils;
 
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.util.HashMap;
import java.util.Map;
 
public class GetStringSpace {
    public static Map<String, Integer> getStringSpaceSize(String text, String fontFamily, int fontSize) {
        fontFamily = fontFamily == null ? "" : fontFamily;
        fontSize = fontSize == 0 ? 10 : fontSize;
        AffineTransform affinetransform = new AffineTransform();
        FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
        Font font = new Font(fontFamily, Font.PLAIN, fontSize);
/*        int textwidth = (int)(font.getStringBounds(text, frc).getWidth());
        int textheight = (int)(font.getStringBounds(text, frc).getHeight());*/
        Map<String, Integer> result = new HashMap<>();
        result.put("width", (int) font.getStringBounds(text, frc).getWidth());
        result.put("height", (int) font.getStringBounds(text, frc).getHeight());
        return result;
    }
}