# Adjust text size based on length of text

**URL:** <https://discourse.processing.org/t/adjust-text-size-based-on-length-of-text/32411>\
**Category:** Coding Questions\
**Created:** [September 25, 2021, 5:47pm UTC](https://discourse.processing.org/t/adjust-text-size-based-on-length-of-text/32411 "2021-09-25T17:47:57Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 28, 2021, 2:41am UTC](https://discourse.processing.org/t/adjust-text-size-based-on-length-of-text/32411/9 "2021-09-28T02:41:44Z")

</div>

To be clear, this is a solution for:

> [@Naison](#):
>
> code that sizes the text based on the length of the text

not:

> [@Naison](#):
>
> create a half-transparent rectangle which “covers” the text

For that second solution, one way is to try making an altered copy of your overflow detector

```auto
boolean isTextOverflow(String txt, float x, float y, float w, float h, float tsize, float leading)

```

…and instead of returning true / false, returns a value textHeight:

```auto
float textHeight(String txt, float x, float y, float w, float h, float tsize, float leading) {
  // ...
}

```

Note that this works much like `isTextOverflow`, and returns the total height for the full text block when laid out – it is _not_ the visible height of a single line. @micycle has a great explainer thread on precisely calculating [visible line height](https://discourse.processing.org/t/a-more-accurate-approach-to-calculating-text-width-text-height/24879).

---

_[View the full topic](https://discourse.processing.org/t/adjust-text-size-based-on-length-of-text/32411)._
