Loading...
Logo
Processing Forum
Hello.

I have looked the forum but no clear answer.

How is it possible to create a text area based on the lines of the text? As in the following example, you may notice that I enter the variables for the text area (x, y, width, height). However, in my case, I would like to know if there is a specific method of calculating the height of the text area automatically based on the number of lines.

Any ideas?

Copy code
  1. String a1;
  2. String a2;

  3. size(200, 200);

  4. a1 = "This is a message written in Processing and it is displayed on the main window";
  5. a2 = "Another message written in Processing and it is displayed on the main window";

  6. text(a1, 10, 10, 150, 100);
  7. text(a2, 10, 100, 150, 100, 101);

Replies(5)

You could use textWidth() to get the length of the string and the line height is textAscent()+textDescent() frm these you cold calculate the number of lines and multiply this by the line height.

The only problem is that it is likely to underestimate the number of lines because it assumes line breaks can occur anywhere in the text including partway through a character.

So the answer is no Processing does not have a method to do this.
This has been asked many times, and there have been lots of answers... mostly in the Beta forums. This one worked for me: http://processing.org/discourse/beta/num_1195937999.html
Thank you all for the responses, as you suggested, I have to make one solution for my problem!
Trying to work out an algorithm that works for different fonts + sizes is quite a mission. Using the textAscent() + textDescent() proved ineffective when working with all the variations. Also none of the old forum post examples take into consideration line breaks '\n'. 

I modified one of the old examples so you pass in the text, the width, and the leading (which MUST be set before calling  textHeight with 'textLeading(someNumber)' function). This has worked pretty well for me so far.


Copy code
  1. // in setup or wherever call setLeading() 
    float myLeading = 24;
    setLeading(myLeading);
    float myTextHeight = textHeight(someString, someWidth, myLeading);

  2. int textHeight(String str, int specificWidth, int leading) {

  3.   // split by new lines first
  4.   String[] paragraphs = split(str, "\n");
  5.   int numberEmptyLines = 0;
  6.   int numTextLines = 0;
  7.   for (int i=0; i < paragraphs.length; i++) {
  8.     // anything with length 0 ignore and increment empty line count
  9.     if(paragraphs[i].length() == 0) {
  10.       numberEmptyLines++;
  11.     } else {      
  12.       numTextLines++;
  13.       // word wrap
  14.       String[] wordsArray = split(paragraphs[i], " ");
  15.       String tempString = "";
  16.       for (int k=0; k < wordsArray.length; k++) {
  17.         if (textWidth(tempString + wordsArray[k]) < specificWidth) {
  18.           tempString += wordsArray[k] + " ";
  19.         }
  20.         else {
  21.           tempString = wordsArray[k] + " ";
  22.           numTextLines++;
  23.         }
  24.       }
  25.     }
  26.   }

  27.   float totalLines = numTextLines + numberEmptyLines;
  28.   return round(totalLines * leading);
  29. }

Hey,
Not sure if anyone's still following this thread but I found the info here really helpful. I posted an example of this in action  here just in case anyone else would like to reuse it. It's a modification of tabbed's algorithm (thanks!), but I placed it in a class named 'Tex' instead and added a few other metrics that might be of use (e.g. number of total lines, etc).