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

Feature Request: Smaller 'steps' #36

Open adambundy opened 5 years ago

adambundy commented 5 years ago

Thanks for this! Great plugin. Would be nice to be able to specify the size of the font-size 'steps', so that, for instance, half-pixel values could result. I've been trying to modify the plugin to do this without much success.

BenEfrati commented 5 years ago

Thanks for this! Great plugin. Would be nice to be able to specify the size of the font-size 'steps', so that, for instance, half-pixel values could result. I've been trying to modify the plugin to do this without much success.

Try this for better precision ('steps')

// Binary search for highest best fit
    var size = low;
    while (low <= high) {
      mid = (high + low) / 2;
      innerSpan.style.fontSize = mid + 'px';
      if(innerSpan.scrollWidth <= originalWidth && (settings.widthOnly || innerSpan.scrollHeight <= originalHeight)){
        size = mid;
        low = mid + 0.1;
      } else {
        high = mid - 0.1;
      }
      // await injection point
    }

image