Find width and height of the text drawn with text()

Hi, I was wondering if it was possible to, say, draw a rectangle around the exact width and height of a block of text on screen.

Let’s say my text() is like follows:

String stringText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

text(stringText, x, y, width-10, height-10);

Because of the 4th and 5th parameter above, this text will wrap around to multiple lines. Is it possible to find the width and height of this text block, so I could draw a rectangle around this with absolutely no breathing space for the furthest letter to the right, or the character with the lowest point on the bottom?

1 Like

you can try to calculate the space you need
and make a rectangle and a estimated text block.

String stringText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

size(200,200);
int texts=10;
textSize(texts);
int textl=texts+5;
textLeading(textl);
float totalw = ceil(textWidth(stringText));
float textw = width-20;
float lines = ceil(totalw/textw); // +1 // to be sure?
float texth = lines*textl;  
println(totalw, lines,  textw, texth);
noStroke();
fill(200,200,0);
rect(10,10,textw, texth);
fill(200,0,0);
text(stringText, 10, 10,textw, texth);

3 Likes
3 Likes

Thank you so much for your help!

1 Like