STRML / textFit

A jQuery-free component that quickly fits single and multi-line text to the width (and optionally height) of its container.
https://textfit.strml.net
637 stars 123 forks source link

Taking scrollbars into account #29

Open me21 opened 7 years ago

me21 commented 7 years ago

Sometimes scrollbars which are initially visible disappear when adjusting text size. This breaks the logic and forces the smallest possible text size.

The issue is here:

    // Binary search for best fit
    while (low <= high) {
      mid = parseInt((low + high) / 2, 10);
      innerSpan.style.fontSize = mid + 'px';
      if(innerSpan.scrollWidth <= originalWidth && (settings.widthOnly || innerSpan.scrollHeight <= originalHeight)){
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }

When text becomes smaller, scrollbars which are initially visible may disappear. Then innerSpan.scrollWidth becomes larger than originalWidth, thus forcing to make the text size even smaller.