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

**URL:** <https://discourse.processing.org/t/find-width-and-height-of-the-text-drawn-with-text/9804>\
**Category:** Coding Questions\
**Created:** [March 31, 2019, 1:58am UTC](https://discourse.processing.org/t/find-width-and-height-of-the-text-drawn-with-text/9804 "2019-03-31T01:58:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingblue](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@kingblue](https://discourse.processing.org/u/kingblue)\
**Post date:** [March 31, 2019, 1:58am UTC](https://discourse.processing.org/t/find-width-and-height-of-the-text-drawn-with-text/9804/1 "2019-03-31T01:58:09Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 31, 2019, 3:40am UTC](https://discourse.processing.org/t/find-width-and-height-of-the-text-drawn-with-text/9804/2 "2019-03-31T03:40:59Z")

</div>

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

```auto
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);

```

 ![SNAG-0048](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9bbdff8f583a79d8eb9e6a7f30107fd570ffd2a1.jpeg)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 31, 2019, 3:48am UTC](https://discourse.processing.org/t/find-width-and-height-of-the-text-drawn-with-text/9804/3 "2019-03-31T03:48:54Z")

</div>

> **[r/processing - Find width and height of the text drawn with text()?](https://www.reddit.com/r/processing/comments/b7isv9/find_width_and_height_of_the_text_drawn_with_text/)**
>
> 0 votes and 1 comment so far on Reddit

---

<div class="post-metadata">

**Author:** ![kingblue](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@kingblue](https://discourse.processing.org/u/kingblue)\
**Post date:** [April 5, 2019, 2:40pm UTC](https://discourse.processing.org/t/find-width-and-height-of-the-text-drawn-with-text/9804/4 "2019-04-05T14:40:48Z")

</div>

Thank you so much for your help!
