Java Utililty Methods Font Text Width

List of utility methods to do Font Text Width

Description

The list of methods to do Font Text Width are organized into topic(s).

Method

StringbuildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name, String surname)
build Windows Compressed Label Name
int charSize = fm.stringWidth("X");
return compressedName(allocatedWidth, charSize, name, surname);
intcharsWidth(char[] data, int off, int len, Font font)
chars Width
return ourGraphics.getFontMetrics(font).charsWidth(data, off, len);
intcharWidth(char ch, Font font)
char Width
return ourGraphics.getFontMetrics(font).charWidth(ch);
intcomputeStringWidth(FontMetrics fm, String str)
Calculates the width of a given string.
return fm.stringWidth(str);
StringcutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)
Returns a shortened string, that fits into the given space with the specified font on the given graphics object considering a possible suffix.
FontMetrics metrics = graphics.getFontMetrics(font);
int textWidth = metrics.stringWidth(text);
StringBuilder actualText = new StringBuilder(text);
int actualTextWidth = metrics.stringWidth(text);
if (textWidth > spaceForText) {
    int suffixWidth = 0;
    if (!suffix.isEmpty()) {
        suffixWidth = metrics.stringWidth(suffix);
...
intfindStringLimit(String aString, Font aFont, int aWidth)
find String Limit
int min = 0;
int max = aString.length();
while (Math.abs(min - max) > 1) {
    int mid = (max + min) / 2;
    int w = getStringLength(aString.substring(0, mid), aFont);
    if (w > aWidth) {
        max = mid;
    } else {
...
intfindWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth, float[] outWidth)
Determines how many characters may be laid in sequence before reaching a setOn width.
int i = 0;
int lineWidth = 0;
for (; i < len; i++) {
    int charWidth = metrics.charWidth(chars[i + off]);
    lineWidth += charWidth;
    if (lineWidth > maxWidth) {
        lineWidth -= charWidth;
        break;
...
doublefontWidth(final Graphics graphics, final Font font, final String str)
Calculate the width in pixels that are necessary to draw the given string in the given font on the given graphics.
return graphics.getFontMetrics(font).getStringBounds(str, graphics).getWidth();
intgetStringWidth(final Font f, final String s)
Determines the width of the given string if it were drawn using the specified font.
return getStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(f), s);
intgetStringWidth(Font font, String text)
Gets the string width.
dummyGFX.setFont(font);
return dummyGFX.getFontMetrics().stringWidth(text);