我的JPanel中有一个区域,以点(0,0)和(宽度,高度)为界。这是一个广场。
我有一个字
String s;
我想找到可用于s
的最大字体大小。现在,我知道有一种方法可以使用FontMetrics并使用for循环来增加字体的大小,直到它不适合该区域。但这样效率很低,并且必须有一种方法来计算给定字体类型的字体大小,例如适合该区域的“Courier”。
BAD方式示例:
Font f = new Font("Courier", Font.PLAIN, 1);
FontMetrics fm = this.getFontMetrics(f); //this is a JPanel
do {
f = new Font("Courier", Font.PLAIN, f.getSize()+1);
fm = this.getFontMetrics(f);
while(fm.stringWidth(s) < width && fm.getHeight() < height);
答案 0 :(得分:1)
我遇到了同样的问题,并且发现了一个稍微优化的解决方案,与迭代所有字体大小相比。我尝试通过调整我添加或减去的差异来收敛到最佳字体大小,直到我发现差异字体大小低于1。
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.black);
if (subtitleFont == null) {
//create rectangle first (from a separate library
int[] rect = matrix.getEnclosingRectangle();
// define the maximum rect for the text
Rectangle2D maxRect = new Rectangle2D.Float(0, 0, w - 7, h - rect[0] - rect[3] - 10);
subtitleX = 0;
subtitleY = 0;
// starting with a very big font due to a high res image
float size = 80f * 4f;
// starting with a diff half the size of the font
float diff = size / 2;
subtitleFont = graphics.getFont().deriveFont(Font.BOLD).deriveFont(size);
FontMetrics fontMetrics = graphics.getFontMetrics(subtitleFont);
Rectangle2D stringBounds = null;
while (Math.abs(diff) > 1) {
subtitleFont = subtitleFont.deriveFont(size);
graphics.setFont(subtitleFont);
fontMetrics = graphics.getFontMetrics(subtitleFont);
stringBounds = fontMetrics.getStringBounds(options.subtitle, graphics);
stringBounds = new Rectangle2D.Float(0f, 0f, (float) (stringBounds.getX() + stringBounds.getWidth()), (float) ( stringBounds.getHeight()));
if (maxRect.contains(stringBounds)) {
if (0 < diff) {
diff = Math.abs(diff);
} else if (diff < 0) {
diff = Math.abs(diff) / 2;
}
} else {
if (0 < diff) {
diff = - Math.abs(diff) / 2;
} else if (diff < 0) {
if (size <= Math.abs(diff)) {
diff = - Math.abs(diff) / 2;
} else {
diff = - Math.abs(diff);
}
}
}
size += diff;
}
subtitleX = (int) ((w/2) - (stringBounds.getWidth() / 2));
subtitleY = (int) (h - maxRect.getHeight() + fontMetrics.getAscent());
}
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.drawString(options.subtitle, subtitleX, subtitleY);
我尝试过不同的图像分辨率和字体大小。它需要10到12次迭代,直到找到适合最大矩形的字体。我希望它会对某人有所帮助。
答案 1 :(得分:1)
是的,我也遇到了同样的问题,我知道你所说的“低效”是什么意思。
我尝试了一种通过字体大小测量字符串单位数的解决方案:1(默认)。
例如:
String line = "This is a test";
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("/var/fonts/times.ttf"));
FontRenderContext ctx = new FontRenderContext(font.getTransform(), false, false);
Rectangle2D rect = font.getStringBounds(line, ctx);
BigDecimal widthUnits = BigDecimal.valueof(rect.getWidth());
那么,你就得到了line的widthUnits by font size 1。如果它除以区域宽度并保留整数部分(向下取整),你将得到水平方向的最大字体大小。
int maxSizeHor = BigDecimal.valueOf(width)
.divide(widthUnits, 1, BigDecimal.ROUND_DOWN)
.intValue();
但是,这还不够。您还应该垂直计算最大字体大小。幸运的是,具有相同字体样式和字体大小的每一行具有相同的高度。如果有多行,您可以使用:
lines.length * each height in font size 1.
例如以下内容:
String[] lines = new String{"This is a test", "Hello World!"};
BigDecimal heightUnits = BigDecimal.valueOf(rect.getHeight())
.multiply(BigDecimal.valueOf(lines.length));
int maxSizeVer = BigDecimal.valueOf(height)
.divide(heightUnits
.multiply(BigDecimal.valueOf(lines.length)),
1,BigDecimal.ROUND_DOWN)
.intValue();
最后,比较并获取最小值作为最大字体大小:
Math.min(maxSizeHor, maxSizeVer)
但是,我遇到了另一个问题:单个字符“i”作为字体大小的输入字符串:1,您将获得宽度单位:0.0。然而,实际上,它不会为零,尽管它是一个非常小的值。所以我将 1000 设置为默认字体大小,并在每个进度后除以 1000。然后我得到非零值。