Java Swing Font Metrics wrapText(String text, FontMetrics metrics, int maxWidth)

Here you can find the source of wrapText(String text, FontMetrics metrics, int maxWidth)

Description

Handy-dandy text wrapping function, adapted from http://www.geekyramblings.net/2005/06/30/wrap-jlabel-text/.

License

Open Source License

Declaration

public static String wrapText(String text, FontMetrics metrics, int maxWidth) 

Method Source Code

//package com.java2s;
/*/*  ww  w.  j av a2  s  . c  om*/
 * This file is part of the FreeSpace Open Installer
 * Copyright (C) 2010 The FreeSpace 2 Source Code Project
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * This program 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 General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import java.awt.FontMetrics;

import java.text.BreakIterator;

import java.util.regex.Pattern;
import javax.swing.SwingUtilities;

public class Main {
    public static final Pattern LEADING_WHITESPACE_PATTERN = Pattern.compile("^\\s+");

    /**
     * Handy-dandy text wrapping function, adapted from
     * http://www.geekyramblings.net/2005/06/30/wrap-jlabel-text/.
     */
    public static String wrapText(String text, FontMetrics metrics, int maxWidth) {
        BreakIterator boundary = BreakIterator.getWordInstance();
        boundary.setText(text);

        StringBuilder line = new StringBuilder();
        StringBuilder paragraph = new StringBuilder();

        // concatenate all words into a new paragraph, adding newlines where appropriate
        for (int start = boundary.first(), end = boundary
                .next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
            String word = text.substring(start, end);

            // if we have an actual newline, start a new line, otherwise append the word to the running total
            if (word.equals("\n"))
                line = new StringBuilder();
            else
                line.append(word);

            // see if all the words have overrun the line width limit yet
            int lineWidth = SwingUtilities.computeStringWidth(metrics, line.toString());
            if (lineWidth > maxWidth) {
                // get rid of any leading whitespace
                word = LEADING_WHITESPACE_PATTERN.matcher(word).replaceFirst("");

                // start a new line in both running strings
                line = new StringBuilder(word);
                paragraph.append("\n");
            }

            paragraph.append(word);
        }

        return paragraph.toString();
    }
}

Related

  1. getTextBounds(FontMetrics fm, String s)
  2. getWidthOfText(FontMetrics fontMetrics, String text)
  3. layoutMultilineCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap, int minLines, int maxLines)
  4. stringWidth(JComponent c, FontMetrics fm, String string)
  5. wrapString(String s, FontMetrics fm, int width)