Java tutorial
//package com.java2s; import java.awt.*; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; public class Main { public static int getFontSize(String text, boolean getWidth) { if (text != null && text.length() > 0) { Font defaultFont = new Font("Montserrat", Font.PLAIN, 13); AffineTransform affinetransform = new AffineTransform(); FontRenderContext frc = new FontRenderContext(affinetransform, true, true); int textWidth = (int) (defaultFont.getStringBounds(text, frc) != null ? defaultFont.getStringBounds(text, frc).getWidth() : 0) + 10; int textHeight = (int) (defaultFont.getStringBounds(text, frc) != null ? defaultFont.getStringBounds(text, frc).getHeight() : 0); return getWidth ? textWidth : textHeight; } return 0; } }