Giter Site home page Giter Site logo

hackur / textfit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from strml/textfit

0.0 2.0 0.0 323 KB

A jQuery-free component that quickly fits single and multi-line text to the width (and optionally height) of its container.

Home Page: http://strml.github.io/examples/textFit.html

HTML 22.05% JavaScript 77.95%

textfit's Introduction

textFit v2.1.0

A fast, jQuery-free text sizing component that quickly fits single and multi-line text to the width (and optionally height) of its container.

Example

Browser Support

textFit supports IE9+, Firefox, Chrome, Opera, and most mobile browsers. If you find an incompatibility, please file an issue.

If you require IE <= 8 support, please use the jQuery version. Functionality is identical between v1.0 and v2.0, the only change was the removal of the jQuery dependency.

Changelog

v2.2.0

  • Throw errors instead of just printing to console when missing height/width.
    • Removed options.suppressErrors. Wrap in try/catch instead if you really need this.
  • Slight refactor.
  • Added automatic build on prepublish.

v2.1.1

  • Fixed a bug with alignVert when reprocessing.
  • Added full UMD shim and published to npm.

v2.1

  • Reworked alignVert.
  • reProcess is now true by default. Set to false if you want to fire-and-forget on potentially processed nodes. This was originally false by default because it was being used in an infinite scrolling list.

v2.0

  • Removed jQuery dependency.

Usage

<div class="box" style="width:300px;height:300px">
  Fit me, I am some text
</div>
// textFit accepts arrays
textFit(document.getElementsByClassName('box'));
// or single DOM elements
textFit(document.getElementsByClassName('box')[0]);
// Use jQuery selectors if you like.
textFit($('#box')[0])

The text will scale until it reaches the horizontal or vertical bounds of the box. Explicit width and height styles are required in order to fit the text.

Advanced Usage

Multiline Strings

If your text has multiple lines, textFit() will automatically detect that and disable white-space: no-wrap! No changes are necessary.

<div class="box" style="width:300px;height:300px">
  This text <br>
  Has multiple lines <br>
  Fit me!
</div>
textFit(document.getElementsByClassName('box'))

If, for some reason, the automatic detection is not working out for you, use the following to explicitly turn on multiLine fitting:

textFit(document.getElementsByClassName('box'), {multiLine: true})

Horizontal/Vertical Centering

<div class="box" style="width:300px;height:300px">
  This text <br>
  Has multiple lines <br>
  And wants to be centered horizontally and vertically<br>
  Fit me!
</div>
textFit(document.getElementsByClassName('box'), {alignHoriz: true, alignVert: true})

Minimum and Maximum Sizes

Sometimes you want to make sure that your text remains sanely sizes if it is very short or very long. textFit has you covered:

<div class="box" style="width:300px;height:300px">
  Short Text
</div>
textFit(document.getElementsByClassName('box'), {minFontSize:10, maxFontSize: 50})

Implementation Details

textFit determines reasonable minimum and maximum bounds for your text. The defaults are listed below.

To ensure accurate results with various font-faces, line-heights, and letter-spacings, textFit resizes the text until it fits the box as accurately as possible. Unlike many similar plugins, textFit uses binary search to find the correct fit, which speeds the process significantly. textFit is fast enough to use in production websites.

textFit() is a synchronous function. Because of this, resizes should be invisible as the render thread does not have a chance to do a layout until completion. Normal processing times should be < 1ms and should not significantly block renders.

Default Settings

From the source, for reference:

settings = {
    alignVert: false, // if true, textFit will align vertically using css tables
    alignHoriz: false, // if true, textFit will set text-align: center
    multiLine: false, // if true, textFit will not set white-space: no-wrap
    detectMultiLine: true, // disable to turn off automatic multi-line sensing
    minFontSize: 6,
    maxFontSize: 80,
    reProcess: true, // if true, textFit will re-process already-fit nodes. Set to 'false' for better performance
    widthOnly: false // if true, textFit will fit text to element width, regardless of text height
};

License

MIT

textfit's People

Contributors

strml avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.