Wraps a single line of text, identifying words by ' '. - Android java.lang

Android examples for java.lang:String Format

Description

Wraps a single line of text, identifying words by ' '.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from  w  w w. jav  a 2s . com*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;
import java.util.ArrayList;
import android.graphics.Paint;

public class Main {
    /**
     * <p>
     * Wraps a single line of text, identifying words by <code>' '</code>.
     * </p>
     * 
     * <p>
     * New lines will be separated by the system property line separator. Very
     * long words, such as URLs will <i>not</i> be wrapped.
     * </p>
     * 
     * <p>
     * Leading spaces on a new line are stripped. Trailing spaces are not
     * stripped.
     * </p>
     * 
     * <pre>
     * WordUtils.wrap(null, *) = null
     * WordUtils.wrap("", *) = ""
     * </pre>
     * 
     * @param str
     *            the String to be word wrapped, may be null
     * @param wrapLength
     *            the column to wrap the words at, less than 1 is treated as 1
     * @return a line with newlines inserted, <code>null</code> if null input
     */
    public static String wrap(String str, int wrapLength) {
        return wrap(str, wrapLength, null, false);
    }

    /**
     * <p>
     * Wraps a single line of text, identifying words by <code>' '</code>.
     * </p>
     * 
     * <p>
     * Leading spaces on a new line are stripped. Trailing spaces are not
     * stripped.
     * </p>
     * 
     * <pre>
     * WordUtils.wrap(null, *, *, *) = null
     * WordUtils.wrap("", *, *, *) = ""
     * </pre>
     * 
     * @param str
     *            the String to be word wrapped, may be null
     * @param wrapLength
     *            the column to wrap the words at, less than 1 is treated as 1
     * @param newLineStr
     *            the string to insert for a new line, <code>null</code> uses
     *            the system property line separator
     * @param wrapLongWords
     *            true if long words (such as URLs) should be wrapped
     * @return a line with newlines inserted, <code>null</code> if null input
     */
    public static String wrap(String str, int wrapLength,
            String newLineStr, boolean wrapLongWords) {
        if (str == null) {
            return null;
        }
        if (newLineStr == null) {
            newLineStr = "\n";// SystemUtils.LINE_SEPARATOR;
        }
        if (wrapLength < 1) {
            wrapLength = 1;
        }
        int inputLineLength = str.length();
        int offset = 0;
        StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);

        while ((inputLineLength - offset) > wrapLength) {
            if (str.charAt(offset) == ' ') {
                offset++;
                continue;
            }
            int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);

            if (spaceToWrapAt >= offset) {
                // normal case
                wrappedLine.append(str.substring(offset, spaceToWrapAt));
                wrappedLine.append(newLineStr);
                offset = spaceToWrapAt + 1;

            } else {
                // really long word or URL
                if (wrapLongWords) {
                    // wrap really long word one line at a time
                    wrappedLine.append(str.substring(offset, wrapLength
                            + offset));
                    wrappedLine.append(newLineStr);
                    offset += wrapLength;
                } else {
                    // do not wrap really long word, just extend beyond limit
                    spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
                    if (spaceToWrapAt >= 0) {
                        wrappedLine.append(str.substring(offset,
                                spaceToWrapAt));
                        wrappedLine.append(newLineStr);
                        offset = spaceToWrapAt + 1;
                    } else {
                        wrappedLine.append(str.substring(offset));
                        offset = inputLineLength;
                    }
                }
            }
        }

        // Whatever is left in line is short enough to just pass through
        wrappedLine.append(str.substring(offset));

        return wrappedLine.toString();
    }

    /**
     * Wraps the given string so that the substrings fit into the the given
     * line-widths. It is expected that the specified lineWidth >=
     * firstLineWidth. The resulting substrings will be added to the given
     * ArrayList. When the complete string fits into the first line, it will be
     * added to the list. When the string needs to be split to fit on the lines,
     * it is tried to split the string at a gap between words. When this is not
     * possible, the given string will be split in the middle of the
     * corresponding word.
     * 
     * 
     * @param value
     *            The string which should be wrapped
     * @param font
     *            The font which is used to display the font
     * @param completeWidth
     *            The complete width of the given string for the specified font.
     * @param firstLineWidth
     *            The allowed width for the first line
     * @param lineWidth
     *            The allowed width for all other lines, lineWidth >=
     *            firstLineWidth
     * @param list
     *            The list to which the substrings will be added.
     */
    private static void wrap(String value, Paint font, float completeWidth,
            float firstLineWidth, float lineWidth, ArrayList<String> list) {
        char[] valueChars = value.toCharArray();
        int startPos = 0;
        int lastSpacePos = -1;
        int lastSpacePosLength = 0;
        int currentLineWidth = 0;
        for (int i = 0; i < valueChars.length; i++) {
            char c = valueChars[i];
            currentLineWidth += font.measureText(String.valueOf(c));// .charWidth(c);
            if (c == '\n') {
                list.add(new String(valueChars, startPos, i - startPos));
                lastSpacePos = -1;
                startPos = i + 1;
                currentLineWidth = 0;
                firstLineWidth = lineWidth;
                i = startPos;
            } else if ((currentLineWidth > firstLineWidth) && (i > 0)) {
                if ((c == ' ') || (c == '\t')) {
                    list.add(new String(valueChars, startPos, i - startPos));
                    startPos = ++i;
                    currentLineWidth = 0;
                    lastSpacePos = -1;
                } else if (lastSpacePos == -1) {
                    if (i > startPos + 1) {
                        i--;
                    }
                    // System.out.println("value=" + value + ", i=" + i +
                    // ", startPos=" + startPos);
                    list.add(new String(valueChars, startPos, i - startPos));
                    startPos = i;
                    currentLineWidth = 0;
                } else {
                    currentLineWidth -= lastSpacePosLength;
                    list.add(new String(valueChars, startPos, lastSpacePos
                            - startPos));
                    startPos = lastSpacePos + 1;
                    lastSpacePos = -1;
                }
                firstLineWidth = lineWidth;
            } else if ((c == ' ') || (c == '\t')) {
                lastSpacePos = i;
                lastSpacePosLength = currentLineWidth;
            }

        }
        // add tail:
        list.add(new String(valueChars, startPos, valueChars.length
                - startPos));

    }
}

Related Tutorials