Java FontMetrics clipString(FontMetrics metrics, int availableWidth, String fullText)

Here you can find the source of clipString(FontMetrics metrics, int availableWidth, String fullText)

Description

Clips string based on specified font metrics and available width (in pixels).

License

Open Source License

Parameter

Parameter Description
metrics Font metrics.
availableWidth Available width in pixels.
fullText String to clip.

Return

The clipped string, which contains the beginning and the end of the input string separated by ellipses (...) in case the string is too long to fit into the specified width, and the origianl string otherwise.

Declaration

public static String clipString(FontMetrics metrics,
        int availableWidth, String fullText) 

Method Source Code

//package com.java2s;
import java.awt.*;

public class Main {
    /**/*from   ww  w  .  j  a va 2s . co  m*/
     * Clips string based on specified font metrics and available width (in
     * pixels). Returns the clipped string, which contains the beginning and the
     * end of the input string separated by ellipses (...) in case the string is
     * too long to fit into the specified width, and the origianl string
     * otherwise.
     * 
     * @param metrics
     *            Font metrics.
     * @param availableWidth
     *            Available width in pixels.
     * @param fullText
     *            String to clip.
     * @return The clipped string, which contains the beginning and the end of
     *         the input string separated by ellipses (...) in case the string
     *         is too long to fit into the specified width, and the origianl
     *         string otherwise.
     */
    public static String clipString(FontMetrics metrics,
            int availableWidth, String fullText) {

        if (metrics.stringWidth(fullText) <= availableWidth)
            return fullText;

        String ellipses = "...";
        int ellipsesWidth = metrics.stringWidth(ellipses);
        if (ellipsesWidth > availableWidth)
            return "";

        String starter = "";

        int w = fullText.length();
        String prevText = "";
        for (int i = 0; i < w; i++) {
            String newStarter = starter + fullText.charAt(i);
            String newText = newStarter + ellipses;
            if (metrics.stringWidth(newText) <= availableWidth) {
                starter = newStarter;
                prevText = newText;
                continue;
            }
            return prevText;
        }
        return fullText;
    }
}

Related

  1. abbreviate(String str, FontMetrics fm, int width)
  2. abbreviateName(String str, FontMetrics fm, int width)
  3. adjustFontSizeToGetPreferredWidthOfLabel(JLabel jLabel, int initialWidth)
  4. alignRight(FontMetrics fm, String string, int align)
  5. clipString(FontMetrics fm, String text, int availableWidth)
  6. clipString(Graphics g, String t, int width)
  7. cutString(String text, FontMetrics fontMetrics, int maxWidth)
  8. fitString(String s, Graphics2D g2, Rectangle2D rect)
  9. getAllStylesSameWidthsFontsFamillyName()