fitty icon indicating copy to clipboard operation
fitty copied to clipboard

Fit to container height

Open gragland opened this issue 6 years ago • 32 comments

Awesome work on this library! I'm looking for something similar but that scales the font size of multi-line text to always fit the container height rather than width. Any thoughts on adding a height option?

gragland avatar Jul 29 '17 23:07 gragland

Thanks! I want to keep the library as small as possible, but this seems like a logical feature for it, I'll see if I can incorporate it without it adding too much additional weight.

rikschennink avatar Jul 30 '17 13:07 rikschennink

@gragland try this https://gist.github.com/rosivanov/d3d9cc739e36f06b96a332d4bdef7d9e

x35a avatar Aug 23 '17 14:08 x35a

@rosivanov Thanks! Will test that out once I get a chance.

gragland avatar Aug 24 '17 03:08 gragland

@rosivanov Thanks!

rikschennink avatar Aug 24 '17 06:08 rikschennink

@gragland @rikschennink You are welcome) Please note that this is very tiny code snippet which does not detect @font-face load and images load. To do this I would recommend libs below https://github.com/JenniferSimonds/FontDetect https://github.com/desandro/imagesloaded

x35a avatar Aug 24 '17 07:08 x35a

@gragland Hey so does this js plugin detect height now, or does that gist linked work? I've been using textSlab.js and it works well for breaking stuff up into multiple lines but if the text being broken up is super long then it makes stuff waaaay to tall.

jcklpe avatar Jun 05 '18 19:06 jcklpe

@thedonquixotic linked gist has to work if you set height for text element

x35a avatar Jun 05 '18 19:06 x35a

@rosivanov Does it work with max-height?

jcklpe avatar Jun 06 '18 17:06 jcklpe

@thedonquixotic guess it should

x35a avatar Jun 06 '18 19:06 x35a

It's been a year since this issue was opened. Are there any plans?

deviousasti avatar Jun 13 '18 15:06 deviousasti

@deviousasti The plans are there, it's the time that is the problem, can't believe it's already been a year.

rikschennink avatar Jun 13 '18 17:06 rikschennink

I just wrote my own:

        function FitText(el) {
            this.element = el;
            this.parent = el.parentElement;
            this.clientHeight = -1;
            this.clientWidth = -1;

            this.fit = function (shouldRecalc) {
                this.parent.style.fontSize = "100px";
                if (shouldRecalc || this.clientWidth < 0) {
                    this.recalc();
                }

                this.parent.style.fontSize =
                    Math.max(10,
                        Math.min(this.clientHeight,
                            Math.floor(this.clientWidth / this.element.clientWidth * 100),
                        )
                    ) + 'px';
            }

            //cache parent width, height
            this.recalc = function () {
                this.clientWidth = this.parent.clientWidth;
                this.clientHeight = this.parent.clientHeight;
            }
        };

HTML

<div class="parent-with-size-defined">
   <span class="fit-text">Some text</span>
</div>

Usage

let f = new FitText(document.querySelector(".fit-text")); f.fit();

deviousasti avatar Jun 13 '18 18:06 deviousasti

Nice! Thanks for posting.

rikschennink avatar Jun 13 '18 19:06 rikschennink

Why not do a pull request to add these changes in to the original plugin?

I'm just a designer learning to code so I get that my JS is super rusty and maybe I'm not understanding the conversation but that seems like a good idea right? If there's a limitation to the plugin, and a fix has been posted then shouldn't that change be pulled in?

jcklpe avatar Jun 20 '18 17:06 jcklpe

@rosivanov thanks for adding that resize code. Unfortunately it doesn't work for me. I'm resizing the test with fitty('.fit'); then I attempt to resize it using scaleFontSize that you made with this: $('.fit').scaleFontSize({minFontsize: 12}); no errors it just doesn't resize to fit the height. Can you elaborate a little more on how it should work? Or am I doing something wrong?

impactcolor avatar Sep 12 '18 22:09 impactcolor

@impactcolor sorry for late reply, hope you already have solved this problem.

x35a avatar Sep 16 '18 14:09 x35a

Any updates on this? This is what I'm also missing right now :)

KoalaBear84 avatar Oct 04 '18 11:10 KoalaBear84

I had a problem where , since it only uses width, the lib made the text too tall for the container.

You can mod the following line where maxSize is the height of the parentNode : for my project, jQuery was already injected so I used

var maxHeight  = $(e.element).closest(".class-of-the-parent-element").height();

e.currentFontSize = Math.min(Math.max(e.minSize, e.availableWidth / e.currentWidth * e.previousFontSize), maxHeight),

iba-jsykes avatar Oct 10 '18 19:10 iba-jsykes

any news?

GuilhermeCouto avatar Jun 04 '19 20:06 GuilhermeCouto

@GuilhermeCouto apart from solutions posted by other devs, no

rikschennink avatar Jun 05 '19 05:06 rikschennink

same issue..please support this feature

MatanMaimon avatar Aug 23 '19 03:08 MatanMaimon

I just wrote my own:

        function FitText(el) {
            this.element = el;
            this.parent = el.parentElement;
            this.clientHeight = -1;
            this.clientWidth = -1;

            this.fit = function (shouldRecalc) {
                this.parent.style.fontSize = "100px";
                if (shouldRecalc || this.clientWidth < 0) {
                    this.recalc();
                }

                this.parent.style.fontSize =
                    Math.max(10,
                        Math.min(this.clientHeight,
                            Math.floor(this.clientWidth / this.element.clientWidth * 100),
                        )
                    ) + 'px';
            }

            //cache parent width, height
            this.recalc = function () {
                this.clientWidth = this.parent.clientWidth;
                this.clientHeight = this.parent.clientHeight;
            }
        };

HTML

<div class="parent-with-size-defined">
   <span class="fit-text">Some text</span>
</div>

Usage

let f = new FitText(document.querySelector(".fit-text")); f.fit();

nice maybe worth to mention to add line-height: 1; in the parent block

MatanMaimon avatar Aug 23 '19 03:08 MatanMaimon

The gist link above does not come up now. Can some one please paste the code snippet here if it works for height? The other piece of code does not set max and min fonts.

How to make the resize working with height as well?

LeelaBasavaiah avatar Jul 10 '20 12:07 LeelaBasavaiah

I am currently in need of this and am willing to contribute a PR if someone can point me in the right direction.

madmod avatar Aug 22 '20 04:08 madmod

@madmod I'm open to merging it, but only if it's fast and doesn't add too much weight.

rikschennink avatar Aug 24 '20 11:08 rikschennink

Please have a look at the bold lines which I added to make it work:

l = function (e) { (e.availableWidth = e.element.parentNode.clientWidth), (e.currentWidth = e.element.scrollWidth), (e.availableHeight = e.element.parentNode.clientHeight), (e.currentHeight = e.element.scrollHeight), (e.previousFontSize = e.currentFontSize), (e.currentFontSize = Math.min(Math.min( Math.max( e.minSize, (e.availableWidth / e.currentWidth) * e.previousFontSize ), e.maxSize ), Math.min( Math.max( e.minSize, (e.availableHeight / e.currentHeight) * e.previousFontSize ), e.maxSize ))), (e.whiteSpace = e.multiLine && e.currentFontSize === e.minSize ? "normal" : "nowrap"); },

LeelaBasavaiah avatar Aug 26 '20 07:08 LeelaBasavaiah

Thanks, this looks like a super small change, I'll take a look at this sometime in the next couple weeks.

rikschennink avatar Aug 27 '20 06:08 rikschennink

@LeelaBasavaiah can you create a public demo of this code on https://codesandbox.io

rikschennink avatar Sep 09 '20 08:09 rikschennink

I would love to see support to make text respect the height of the container in fitty.js, that would make it the best text fitting solution I have seen yet :+1:

cb109 avatar Jun 02 '21 10:06 cb109

Arrived here in search of a way to get height fitting working. Would also love to see it.

Corgi avatar Jan 01 '23 22:01 Corgi