Java Font Text Width findStringLimit(String aString, Font aFont, int aWidth)

Here you can find the source of findStringLimit(String aString, Font aFont, int aWidth)

Description

find String Limit

License

Open Source License

Declaration

private static int findStringLimit(String aString, Font aFont, int aWidth) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Font;

import java.awt.font.FontRenderContext;

import java.awt.geom.AffineTransform;

public class Main {
    protected final static FontRenderContext mFontRenderContext = new FontRenderContext(new AffineTransform(),
            false, false);//from  w ww. j  ava  2  s. c o m

    private static int findStringLimit(String aString, Font aFont, int aWidth) {
        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);

            //System.out.printf("%d\t%d\t%d\t%d\t%d\n", min, max, mid, aWidth, w);

            if (w > aWidth) {
                max = mid;
            } else {
                min = mid;
            }
        }

        return min;
    }

    public static int getStringLength(String aString, Font aFont) {
        return (int) aFont.getStringBounds(aString, mFontRenderContext).getWidth();
    }
}

Related

  1. buildWindowsCompressedLabelName(int allocatedWidth, FontMetrics fm, String name, String surname)
  2. charsWidth(char[] data, int off, int len, Font font)
  3. charWidth(char ch, Font font)
  4. computeStringWidth(FontMetrics fm, String str)
  5. cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)
  6. findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth, float[] outWidth)
  7. fontWidth(final Graphics graphics, final Font font, final String str)
  8. getStringWidth(final Font f, final String s)
  9. getStringWidth(Font font, String text)