Skip to content

Conversation

@samueljim
Copy link

  • Added stopOverflow. If there is too much text then the text will still overflow outside of the div at the smallest font size. Setting stopOverflow to true will add a .overflow class to the selected div if the text is overflowing. It also adds CSS which hides the text that is overflowing in order to keep the design of the page.
  • Added maxLines. Similar to stopOverflow but instead it adds the class if theres too many lines of text. This setting takes an input of false or a number of lines to apply the limit at. This needs to be improved but it's still useful to have now.
  • Added fontUnit. Changes the unit used in the minFontSize and maxFontSize. This allows units such as cm, mm, in, pt, pc, em, vw, vh, % and rem to be used instead of px.
  • Added fontChangeSize. This changes the amount of the font is changed by when trying to find the final font size. The default is 1 but changing this to a decimal is sometimes needed. When using rem or em font units 0.1 or 0.01 is a recommended change size.

Please let me know if you have any questions :)

* Added `stopOverflow`. If there is too much text then the text will still overflow outside of the div at the smallest font size. Setting stopOverflow to true will add a `.overflow` class to the seleted div if the text is overflowing. It also adds CSS which hides the text that is overflowing in order to keep the design of the page.
* Added `maxLines`. Similar to stopOverflow but instead it adds the class if there is too many lines of text. This setting takes an input of `false` or a number of lines to apply the limit at. This needs to be improved but it's still useful to have now.
* Added `fontUnit`. Changes the unit used in the minFontSize and maxFontSize. This allows units such as cm, mm, in, pt, pc, em, vw, vh, % and rem to be used instead of px.
* Added `fontChangeSize`. This changes the amount of the font is changed by when trying to find the final font size. The default is 1 but changing this to a decimal is sometimes needed. When using rem or em font units 0.1 or 0.01 is a recommended change size.
@samueljim samueljim changed the title 4 new settings and fixed examples font unit, overflow updates and fixed examples Jan 21, 2021
Copy link
Owner

@STRML STRML left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A few minor bits here. This is a pretty large change without a ton of code - bravo for that!

Please exclude the example files. I need to properly .gitignore them but they're only meant for gh-pages publishing.


if (settings.maxLines) {
if (Number.isInteger(settings.maxLines)) {
const computedStyle = window.getComputedStyle(innerSpan);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid es6 for compatibility's sake

const fontSize = parseFloat(computedStyle.getPropertyValue('font-size'));
// If line-height is normal, we multiply the element's font-size with 1.14
if ( isNormal ) innerSpan.style.lineHeight = (fontSize * 1.14) + 'px';
const lineHeight = parseFloat(computedStyle.getPropertyValue('line-height'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const lineHeight = parseFloat(computedStyle.getPropertyValue('line-height'));
const lineHeightFloat = parseFloat(computedStyle.getPropertyValue('line-height'));
const lineHeight = Number.isFinite(lineHeightFloat) ? lineHeightFloat : parseFloat(computedStyle.getPropertyValue('font-size') * 1.14;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically can remove the double-read here and simplify the logic a bit by simply assuming fontSize * 1.14 if lineHeight is unreadable

// Setting the element's max-height
const limitHeight = (lineHeight * settings.maxLines);

innerSpan.style.maxHeight = limitHeight + 'px';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible try to batch all reads before all writes to avoid layout thrashing

@JacksonWolff
Copy link

JacksonWolff commented Jul 14, 2022

Really interested on this PR @STRML, any plans for bring the fontUnit options soon? Can I help you on that?

@araeubig
Copy link

araeubig commented Apr 6, 2025

Are there any news on that? It would be great to use font units.

@samueljim
Copy link
Author

Are there any news on that? It would be great to use font units.

i should fix PR this but if you need font units and more i've made a package that can do heaps more as a web component
https://www.npmjs.com/package/textfit-web-component

@araeubig
Copy link

araeubig commented Apr 7, 2025

Thanks for your quick answer. I will give it a try very soon.

@araeubig
Copy link

araeubig commented Apr 7, 2025

I tested the web component and it works with normal use, but for me it isn't working. In my case the content of the specific field will be updated every 30 seconds and therefore i have to trigger the resizing after updating the content of the span. Would it be possible with the web component or do you have another idea?

@araeubig
Copy link

araeubig commented Apr 7, 2025

I think i was too quick.... Actually it seems to work - i will give you a more detailed feedback until tomorrow. Regards André

@samueljim
Copy link
Author

The web component should work, but I've not done much testing on it outside of my use cases.
It should detect that the content inside of it has changed and run again but if not you can trigger the event using the runValidation property on the object.

document.querySelector('#selector-for-demo').runValidation();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants