79651635

Date: 2025-06-03 17:09:31
Score: 1
Natty:
Report link

To calculate how much space a text will occupy, you can use:

   String word = "My word";
   Font font = new Font(  "Arial", Font.PLAIN, 14 );
   Canvas canvas = new Canvas();
   FontMetrics fm = canvas.getFontMetrics( font );
   int width = fm.stringWidth( word ); 

Now let's see how to apply it to a concrete case:

public class TextFormat {

   FontMetrics fm;

   void initialize() {
      Font font = new Font( "Arial", Font.PLAIN, 14 );
      Canvas canvas = new Canvas();
      fm = canvas.getFontMetrics( font );
   }

     // this method is very simple, it just creates an array of phrases, dividing the 
     // original string in lines, and then iterates over the array creating an array of 
     // words, and adding in each iteration to **out**, the return of the **cut** method.  
   String formatText( String imitialInput, int maxWidth ) {
      String[] lines = imitialInput.split( "\n" );
      String out = "";
      for( int i = 0; i < lines.length; i ++ ) {
         String[] words = lines[ i ].split( " " );
         out += cut( words, 0, maxWidth );
      }
      return out;
   }

     // this method makes use of recursion, it iterates over the array of words, adding 
     // to **aux** the content of the variable **space** (in the first iteration it will 
     // be **“”** and in the successive **“ ”**), concatenated with the words of the 
     // array, then verifies if the length of this one exceeds the limit, in which case,  
     // it returns the trimmed value of **aux** (it removes the last added word) plus the 
     // string **"\n ”** the return of **cut**, passing as parameters, the array of 
     // words, the value of **i** (so that it continues oterating from the first word not 
     // added) and **maxWidth**, and the last line, it returns the possible remainder.
   String cut(  String[] words, int init, int maxWidth ) {
      String aux = "", 
              space = "";      
      for( int i = init; i < words.length; i++ ) {
         aux += space + words[ i ];
         int width = fm.stringWidth( aux );
         if( width > maxWidth ) {
            return aux.substring( 0, aux.length() - words[ i ].length() - 1 )
                    + "\n" + cut( words, i, maxWidth );
         }
         if( space.isEmpty( )) space = " ";
      }
      return aux + "\n";
   }
}

class Main {

   String text = "Hi! Please provide code you have tried this with."
           + " Please don't use StackOverflow as a code delivery service.\n"
           + "If you provide a minimal reproducible example we can help debug. "
           + "See how to ask";

   void init() {
      TextFormat tf = new TextFormat();
      tf.initialice();
      tf.formatText( text, 200 ); 
   }

   public static void main( String x[] ) {
      new Main().init();
   }
}

Out:

Hi👋🏻! Please provide code you have tried this with. Please don't use StackOverflow as a code delivery service. If you provide a minimal reproducible example we can help debug. See how to ask

Reasons:
  • Blacklisted phrase (1): StackOverflow
  • Whitelisted phrase (-1.5): you can use
  • RegEx Blacklisted phrase (2.5): Please provide code you
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Marce Puente