Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ settings = {
maxFontSize: 80,
reProcess: true, // if true, textFit will re-process already-fit nodes. Set to 'false' for better performance
widthOnly: false, // if true, textFit will fit text to element width, regardless of text height
heightOnly: false, // if true, textFit will fit text to element height, regardless of text width
alignVertWithFlexbox: false, // if true, textFit will use flexbox for vertical alignment
};
```
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare module "textfit" {
maxFontSize?: number;
reProcess?: boolean;
widthOnly?: boolean;
heightOnly?: boolean;
alignVertWithFlexbox?: boolean;
}

Expand Down
14 changes: 9 additions & 5 deletions textFit.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
maxFontSize: 80,
reProcess: true, // if true, textFit will re-process already-fit nodes. Set to 'false' for better performance
widthOnly: false, // if true, textFit will fit text to element width, regardless of text height
heightOnly: false, // if true, textFit will fit text to element height, regardless of text width
alignVertWithFlexbox: false, // if true, textFit will use flexbox for vertical alignment
};

Expand Down Expand Up @@ -99,13 +100,16 @@
originalHeight = innerHeight(el);

// Don't process if we can't find box dimensions
if (!originalWidth || (!settings.widthOnly && !originalHeight)) {
if(!settings.widthOnly)
if ((!settings.heightOnly && !originalWidth) || (!settings.widthOnly && !originalHeight)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Should be an added error in case both widthOnly and heightOnly are set

if(!settings.widthOnly && !settings.heightOnly)
throw new Error('Set a static height and width on the target element ' + el.outerHTML +
' before using textFit!');
else
else if (!settings.heightOnly)
throw new Error('Set a static width on the target element ' + el.outerHTML +
' before using textFit!');
else if (!settings.widthOnly)
throw new Error('Set a static height on the target element ' + el.outerHTML +
' before using textFit!');
}

// Add textFitted span inside this container.
Expand Down Expand Up @@ -158,8 +162,8 @@
innerSpan.style.fontSize = mid + 'px';
var innerSpanBoundingClientRect = innerSpan.getBoundingClientRect();
if (
innerSpanBoundingClientRect.width <= originalWidth
&& (settings.widthOnly || innerSpanBoundingClientRect.height <= originalHeight)
(settings.heightOnly || innerSpanBoundingClientRect.width <= originalWidth)
&& (settings.widthOnly || innerSpanBoundingClientRect.height <= originalHeight)
) {
size = mid;
low = mid + 1;
Expand Down
2 changes: 1 addition & 1 deletion textFit.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.