List of utility methods to do Font Text Size
Number | getSize(Font font) get Size return getAsNumber(font, TextAttribute.SIZE);
|
Dimension | getStringDimension(Component comp, String str) Calculates the dimension of a string FontMetrics fm = comp.getFontMetrics(comp.getFont()); return new Dimension(fm.stringWidth(str), fm.getHeight()); |
Dimension | getStringDimensions(Graphics2D graphics, Font font, String[] stringsByLine) get String Dimensions FontMetrics fontMetrics = graphics.getFontMetrics(font); int overallWidth = 0; int overallHeight = 0; for (String string : stringsByLine) { int width = fontMetrics.stringWidth(string); int height = fontMetrics.getHeight(); overallWidth = Math.max(overallWidth, width); overallHeight += height; ... |
int | getStringDisplaySize(String input) get String Display Size GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); Font font = new Font("Arial", Font.PLAIN, 10); FontMetrics fm = bi.getGraphics().getFontMetrics(font); return fm.stringWidth(input); |
Dimension | getStringSize(final Graphics g, final Font font, final String text) get String Size final FontMetrics metrics = g.getFontMetrics(font); final int hgt = metrics.getHeight(); final int adv = metrics.stringWidth(text); return new Dimension(adv + 2, hgt + 2); |
Dimension | getStringSize(Graphics g, String text, Font font) get String Size if (text == null || text.isEmpty()) return new Dimension(); FontMetrics fm; if (font == null) { fm = g.getFontMetrics(); } else { fm = g.getFontMetrics(font); int w = fm.stringWidth(text); int h = Math.max(fm.getHeight(), fm.getAscent() + fm.getDescent()); return new Dimension(w, h); |
Dimension | getStringSize(String textToMeasure, Font displayFont) Measures the size of the input string, taking into account the current font and newlines. final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); final String[] strings = textToMeasure.split("\n"); double totalHeight = 0; double maxWidth = 0; for (String s : strings) { Rectangle2D stringBounds = displayFont.getStringBounds(s, fontRenderContext); maxWidth = Math.max(maxWidth, stringBounds.getWidth()); totalHeight += stringBounds.getHeight(); ... |
Rectangle2D | getStringSizeForFont(final String string, final Font font) get String Size For Font if (font == null) { return null; } else if (string == null || string.length() == 0) { return new Rectangle2D.Double(0, 0, 0, 0); } else { final FontRenderContext fontRenderContext = new FontRenderContext(new AffineTransform(), true, true); final double width = font.getStringBounds(string, fontRenderContext).getWidth(); final double height = font.getStringBounds(string, fontRenderContext).getHeight(); ... |