- Notifications
You must be signed in to change notification settings - Fork122
A jQuery-free component that quickly fits single and multi-line text to the width (and optionally height) of its container.
STRML/textFit
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Afast, dependency-free text sizing component that quickly fits single and multi-line text to the width and/or height of its container.
textFit
is:
- Fast, using binary search to quickly fit text to its container in
log n
time, making it far faster than most solutions.- Most fits are
<1ms
. See theimplementation details.
- Most fits are
- Dependency-free.
- Small.
4.1KB
minified and1.5KB
gzipped. - Supports both horizontal and vertical centering, including vertical centering with Flexbox for maximum accuracy.
- Supports any font face, padding, and multiline text.
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 thejQuery version.Functionality is identical between v1.0 and v2.0, the only change was the removal of the jQuery dependency.
- Fix#20 - properly iterate over
HTMLCollection
objects.
- Added
alignVertWithFlexbox
. This does better vertical alignment and fixes #14.
- Throw errors instead of just printing to console when missing height/width.
- Removed
options.suppressErrors
. Wrap intry/catch
instead if you really need this.
- Removed
- Slight refactor.
- Added automatic build on prepublish.
- Fixed a bug with
alignVert
when reprocessing. - Added full UMD shim and published to npm.
- Reworked alignVert.
reProcess
is nowtrue
by default. Set tofalse
if you want to fire-and-forget on potentiallyprocessed nodes. This was originally false by default because it was being used in an infinite scrolling list.
- Removed jQuery dependency.
<divclass="box"style="width:300px;height:300px"> Fit me, I am some text</div>
// textFit accepts arraystextFit(document.getElementsByClassName('box'));// or single DOM elementstextFit(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.
If your text has multiple lines, textFit() will automatically detect that and disable white-space: no-wrap!No changes are necessary.
<divclass="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 onmultiLine fitting:
textFit(document.getElementsByClassName('box'),{multiLine:true})
<divclass="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})
Sometimes you want to make sure that your text remains sanely sizes if it is very short or very long. textFithas you covered:
<divclass="box"style="width:300px;height:300px"> Short Text</div>
textFit(document.getElementsByClassName('box'),{minFontSize:10,maxFontSize:50})
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 textuntil it fits the box as accurately as possible. Unlike many similar plugins, textFit usesbinary search tofind the correct fit, which speeds the process significantly. textFit is fast enough to use in productionwebsites.
textFit()
is a synchronous function. Because of this, resizes should be invisible as the render thread does nothave a chance to do a layout until completion. Normal processing times should be < 1ms and should not significantlyblock renders.
From the source, for reference:
settings={alignVert:false,// if true, textFit will align vertically using css tablesalignHoriz:false,// if true, textFit will set text-align: centermultiLine:false,// if true, textFit will not set white-space: no-wrapdetectMultiLine:true,// disable to turn off automatic multi-line sensingminFontSize:6,maxFontSize:80,reProcess:true,// if true, textFit will re-process already-fit nodes. Set to 'false' for better performancewidthOnly:false,// if true, textFit will fit text to element width, regardless of text heightalignVertWithFlexbox:false,// if true, textFit will use flexbox for vertical alignment};
MIT
About
A jQuery-free component that quickly fits single and multi-line text to the width (and optionally height) of its container.