Skip to content

Commit 457b5d3

Browse files
readme
1 parent 724d0b0 commit 457b5d3

File tree

3 files changed

+106
-22
lines changed

3 files changed

+106
-22
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.

src/index.html

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,18 @@
4141
root.append(el);
4242
})
4343
});
44-
jQuery('.myText').toArray().forEach(function (el) {
45-
fancyTextFill.fillParentContainer(el, {
46-
minFontSize: 6,
47-
maxFontSize: 26
48-
});
44+
jQuery('.myText').fancyTextFill({
45+
minFontSize: 6,
46+
maxFontSize: 26
4947
});
5048
jQuery("#slider").on("input change", function (event) {
5149
var r = Number.parseInt(event.currentTarget.value)
52-
jQuery('.myText').toArray().forEach(function (el) {
53-
var width = 100 + r;
54-
el.parentElement.style.width = width + "px";
55-
fancyTextFill.fillParentContainer(el, {
56-
minFontSize: 6,
57-
maxFontSize: 26
58-
});
50+
jQuery('.box').toArray().forEach(function (el) {
51+
el.style.width = (100 + r) + "px";
52+
});
53+
jQuery('.myText').fancyTextFill({
54+
minFontSize: 6,
55+
maxFontSize: 26
5956
});
6057
});
6158
}, false);

src/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,24 @@ function fontInfo(element: HTMLElement): FontInfo {
1818
}
1919

2020
export interface Options {
21-
maxWidth: number;
22-
maxHeight: number;
23-
minFontSize: number;
24-
maxFontSize: number;
25-
multiline: boolean;
21+
maxWidth?: number;
22+
maxHeight?: number;
23+
minFontSize?: number;
24+
maxFontSize?: number;
25+
multiline?: boolean;
2626
}
2727

28-
export function fillParentContainer(element: HTMLElement, opts: Options): void {
28+
export function fillParentContainer(element: HTMLElement, opts: Options = {}): void {
2929
let {
30-
minFontSize,
31-
maxFontSize,
32-
maxWidth = element.parentElement!.clientWidth,
33-
maxHeight = element.parentElement!.clientHeight,
30+
minFontSize = 4,
31+
maxFontSize = 40,
32+
maxWidth = null,
33+
maxHeight = null,
3434
multiline = true
3535
} = opts;
36+
maxWidth = maxWidth || element.parentElement!.clientWidth;
37+
maxHeight = maxHeight || element.parentElement!.clientHeight;
38+
maxFontSize = Number(maxFontSize) > 0? maxFontSize : maxHeight;
3639
let text = element.textContent || '';
3740
let { font } = fontInfo(element);
3841

0 commit comments

Comments
 (0)