|
| 1 | +# FancyTextFill |
| 2 | + |
| 3 | +Fast implementation for resizing text to fill its container. |
| 4 | +It computes the optimal font-size needed to match a text to specific width and height. |
| 5 | +It's also available as a jquery plugin. |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install --save fancy-textfill |
| 11 | +# or you can use yarn (yarn add fancy-textfill) |
| 12 | +``` |
| 13 | + |
| 14 | +## Example |
| 15 | + |
| 16 | +```html |
| 17 | +<!-- In case you're using it as a jquery plugin --> |
| 18 | +<script src="jquery.min.js"></script> |
| 19 | +<script src="fancy-text-fill.jQuery.js"></script> |
| 20 | +<!-- Or you can use it without jquery --> |
| 21 | +<script src="fancy-text-fill.js"></script> |
| 22 | +<!-- Example setup --> |
| 23 | +<style> |
| 24 | + .container { |
| 25 | + width: 200px; |
| 26 | + height: 50px; |
| 27 | + } |
| 28 | +</style> |
| 29 | +<div class="container"> |
| 30 | + <span class="myText">Hello darkness, my old friend.</span> |
| 31 | +</div> |
| 32 | +<div class="container"> |
| 33 | + <span class="myText">I've come to talk with you again.</span> |
| 34 | +</div> |
| 35 | +``` |
| 36 | + |
| 37 | +You can either use it on bare dom elements or on jquery objects. |
| 38 | + |
| 39 | +```js |
| 40 | +// Without jquery |
| 41 | +document.getElementsByClassName('myText') |
| 42 | + .forEach(function (el) { |
| 43 | + fancyTextFill.fillParentContainer(el, { |
| 44 | + minFontSize: 6, |
| 45 | + maxFontSize: 26 |
| 46 | + }); |
| 47 | + }); |
| 48 | +// With jquery |
| 49 | +$('.myText').fancyTextFill({ |
| 50 | + minFontSize: 6, |
| 51 | + maxFontSize: 26 |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +## Options |
| 56 | + |
| 57 | +| Name | Description | Default value | |
| 58 | +|-------------|-------------|---------------| |
| 59 | +| minFontSize | Minimal font size (in pixels). The text will shrink up to this value. | 4 | |
| 60 | +| maxFontSize | Maximum font size (in pixels). The text will stretch up to this value. If it is `null` or a negative number (`maxFontSize <= 0`), the text will stretch to as big as the container can accommodate. | 40 | |
| 61 | +| maxWidth | Explicit width to resize. Defaults to the container's width. | `null` | |
| 62 | +| maxHeight | Explicit height to resize. Defaults to the container's height. | `null` | |
| 63 | +| multiline | Will only resize to the width restraint when set to `false` | true | |
| 64 | + |
| 65 | +## How does it compare to... |
| 66 | + |
| 67 | +1. [jquery-textfill](https://github.com/jquery-textfill/jquery-textfill) |
| 68 | +> **Performance!** fancy-TextFill implements the same features while being way faster than the original jquery plugin. |
| 69 | +2. [BigText](https://github.com/zachleat/BigText) |
| 70 | +> BigText doesn't support multiple lines. |
| 71 | +
|
| 72 | +## Unit tests |
| 73 | + |
| 74 | +```bash |
| 75 | +# Run chrome driver |
| 76 | +chromedriver --port=4444 --url-base=wd/hub |
| 77 | +# In another console |
| 78 | +npm run build:dev |
| 79 | +npm run test |
| 80 | +``` |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +This code is licensed under the _MIT License_. See file LICENSE for more details. |
0 commit comments