TextWrapper.java
/*
* Copyright © 2011 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.wordpress.helpers;
import java.util.Vector;
import javax.microedition.lcdui.Font;
/**
* Utility for wrapping text to a certain width.
* Brute force method.
*/
public class TextWrapper {
/**
* Wraps text
* @param text Text to be wrapped
* @param wrapWidth Max width of one line in pixels
* @param font Font to be used in calculating
* @return
*/
public static Vector wrapTextToWidth(String text, int wrapWidth, Font font) {
int lineWidth = 0;
Vector lines = new Vector();
StringBuffer line = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
int charWidth = font.charWidth(c);
lineWidth += charWidth;
if (lineWidth < wrapWidth) {
line.append(c);
} else {
lines.addElement(line.toString());
line = new StringBuffer();
line.append(c);
lineWidth = charWidth;
}
}
lines.addElement(line.toString());
return lines;
}
}