• Stars
    star
    598
  • Rank 74,853 (Top 2 %)
  • Language
    JavaScript
  • Created about 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

textFit - Hassle-Free Text Fitting

A fast, dependency-free text sizing component that quickly fits single and multi-line text to the width and/or height of its container.

Example

Capabilities

textFit is:

  • Fast, using binary search to quickly fit text to its container in log n time, making it far faster than most solutions.
  • Dependency-free.
  • Small. 4.1KB minified and 1.5KB gzipped.
  • Supports both horizontal and vertical centering, including vertical centering with Flexbox for maximum accuracy.
  • Supports any font face, padding, and multiline text.

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.3.1

  • Fix #20 - properly iterate over HTMLCollection objects.

v2.3.0

  • Added alignVertWithFlexbox. This does better vertical alignment and fixes #14.

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
    alignVertWithFlexbox: false, // if true, textFit will use flexbox for vertical alignment
};

License

MIT

More Repositories

1

strml.net

STRML: Projects & Work
JavaScript
2,576
star
2

react-router-component

Declarative router component for React.
JavaScript
874
star
3

keyMirror

Simple util to create an object with its keys mirrored as values. Standalone port of react/lib/keyMirror.
JavaScript
386
star
4

react-localstorage

Simple componentized localstorage implementation for Facebook's React.
JavaScript
288
star
5

JSXHint

Wrapper around JSHint for linting JSX files. 100% compatible with existing tools using JSHint.
JavaScript
274
star
6

react-router-component-transition

Example code for router which does animated page transitions using ReactCSSTranstionGroup
JavaScript
244
star
7

securesha.re-client

Client-side files for Securesha.re, a simple end-to-end encrypted file sharing website.
JavaScript
107
star
8

Healthcare.gov-Marketplace

Community-driven fixes to healthcare.gov's unstable marketplace.
JavaScript
85
star
9

json-to-flow

Convert model schemata into Flow types (.js.flow)
JavaScript
64
star
10

mongoose-filter-denormalize

Simple filtering and denormalization for Mongoose.
JavaScript
53
star
11

async-limiter

A minimal library for throttling async concurrency.
JavaScript
30
star
12

react-document-events

Declarative method for binding handlers to document and window - and cleaning them up.
JavaScript
25
star
13

Imgur-to-Gfycat

Chrome extension that replaces gifs hosted by imgur to HTML5-optimized video converted and hosted by gfycat.
JavaScript
23
star
14

init

Configs
Lua
22
star
15

wayback-machine-machine

Teleport your browser back in time.
JavaScript
18
star
16

babel-preset-es2015-maybe-webpack

JavaScript
13
star
17

backbone.routeNotFound

Simple plugin that fires an event to let the application know that no routes matched, so you can fire a custom 404 handler.
JavaScript
11
star
18

backbone.queryRouter

Drop-in Backbone.Router replacement with support for listening to query parameters.
JavaScript
9
star
19

react-addons

Simple packaging of react addons to avoid fiddly 'react/addons' npm module.
JavaScript
8
star
20

jquery.scrollingShadows

A simple jQuery plugin that creates a shadow on the edge of a scrolling box, indicating that there is more content below.
JavaScript
8
star
21

presentations

JavaScript
8
star
22

forward-emitter

Forward events from any Node EventEmitter to another EventEmitter.
JavaScript
6
star
23

bootstrap-components

Web Components for Bootstrap.
4
star
24

fluxxor-autobind

AutoBind plugin for Fluxxor to help prevent spaghetti props wiring.
JavaScript
4
star
25

js-slack-bot

REPL in Slack.
JavaScript
4
star
26

wheresMyMoneyCampBX

CampBX Auto-Withdrawal Script. Tries to recover your coins.
Python
3
star
27

bitmex-blockclock

Push BitMEX prices to your Blockclock
JavaScript
3
star
28

healthcare.gov

JavaScript
2
star
29

strml.github.com

JavaScript
1
star
30

yarn-link-bug

Reproduction of a yarn --link-duplicates bug
1
star
31

flux-connect

A Redux-style @connect decorator for any Flux app, with proper Flow typing.
JavaScript
1
star
32

openbazaar-ui

CSS
1
star
33

bitmex-DTC

DTC to BitMEX API adapter
OCaml
1
star
34

parse-stream

Parse streams of binary data of arbitrary lengths, handling broken/incomplete chunks.
JavaScript
1
star