Java FontMetrics getStringForMaxWidth(FontMetrics fm, String s, int maxWidth)

Here you can find the source of getStringForMaxWidth(FontMetrics fm, String s, int maxWidth)

Description

Returns a string that fit into the maximum width based on the parameter string.

License

Open Source License

Parameter

Parameter Description
fm The metrics of the font to be calculated with.
s The string to calculate its width.
maxWidth The maximum width the string is allowed to take.

Return

The orignal string if it fit into the maximum width otherwise it returns a cut string extended with "..." string.

Declaration

public static String getStringForMaxWidth(FontMetrics fm, String s, int maxWidth) 

Method Source Code

//package com.java2s;
/**/*  w  w w  .  j  a  va  2s  .c o m*/
 * Squidy Interaction Library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * Squidy Interaction Library is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Squidy Interaction Library. If not, see
 * <http://www.gnu.org/licenses/>.
 * 
 * 2009 Human-Computer Interaction Group, University of Konstanz.
 * <http://hci.uni-konstanz.de>
 * 
 * Please contact info@squidy-lib.de or visit our website
 * <http://www.squidy-lib.de> for further information.
 */

import java.awt.FontMetrics;

public class Main {
    /**
     * Returns a string that fit into the maximum width based on the parameter
     * string. It returns the orignal string if it fit into the maximum width
     * otherwise it returns a cut string extended with "..." string.
     * 
     * @param fm
     *            The metrics of the font to be calculated with.
     * @param s
     *            The string to calculate its width.
     * @param maxWidth
     *            The maximum width the string is allowed to take.
     * @return The orignal string if it fit into the maximum width otherwise it
     *         returns a cut string extended with "..." string.
     */
    public static String getStringForMaxWidth(FontMetrics fm, String s, int maxWidth) {

        boolean changed = false;
        for (int nameWidth = fm.stringWidth(s); nameWidth > maxWidth; nameWidth = fm.stringWidth(s.concat("..."))) {
            s = s.substring(0, s.length() - 1);
            changed = true;
        }

        if (changed) {
            return s.concat("...");
        }
        return s;
    }
}

Related

  1. getMaxFittingFontSize(Graphics g, Font font, String string, Dimension size)
  2. getMaxFontHeight(final java.awt.Font font)
  3. getMonospacedFontNames()
  4. getMonospacedFontsFamillyName()
  5. getPrefSize(FontMetrics fm, String keyTip)
  6. getStringImage(Font font, String... strs)
  7. getStringLengthInPixels(String s, Graphics g)
  8. getStringRect(Font f, String s)
  9. getTextCenterShear(final FontMetrics fm, final String text)