Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Paint; public class Main { final static String SYSTEM_NEWLINE = "\n"; private static String wrap(String s, float width, Paint p) { String[] str = s.split("\\s"); // regex StringBuilder smb = new StringBuilder(); // save memory smb.append(SYSTEM_NEWLINE); for (String element : str) { float length = p.measureText(element); String[] pieces = smb.toString().split(SYSTEM_NEWLINE); try { if ((p.measureText(pieces[pieces.length - 1]) + length) > width) { smb.append(SYSTEM_NEWLINE); } } catch (Exception e) { } smb.append(element + " "); } return smb.toString().replaceFirst(SYSTEM_NEWLINE, ""); } }