From 60eac2aa29e8261fbcf3cd826ac50de00108acfb Mon Sep 17 00:00:00 2001 From: dailichen Date: Mon, 26 Aug 2013 12:37:54 +0800 Subject: [PATCH] allow flexible text length --- example.html | 4 ++++ jquery.fittext.js | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/example.html b/example.html index c6a2aee..af97ee0 100644 --- a/example.html +++ b/example.html @@ -44,6 +44,8 @@

Squeeze with FitText

Squeeze with FitText

Squeeze with FitText

+

Squeeze with FitText For Long String

+

Squeeze with above

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

@@ -54,6 +56,8 @@

Squeeze with FitText

$("#fittext1").fitText(); $("#fittext2").fitText(1.2); $("#fittext3").fitText(1.1, { minFontSize: '50px', maxFontSize: '75px' }); + $("#fittext4").fitText(); + $("#fittext5").fitText(); diff --git a/jquery.fittext.js b/jquery.fittext.js index 080b82e..279410e 100644 --- a/jquery.fittext.js +++ b/jquery.fittext.js @@ -25,9 +25,13 @@ // Store the object var $this = $(this); - // Resizer() resizes items based on the object width divided by the compressor * 10 + $this.css("white-space", "nowrap"); + + var fontWidthRate = $this.css("float", "left").width() / parseFloat($this.css("float", "none").css("font-size")); + + // Resizer() resizes items based on the object width divided by the compressor * fontWidthRate var resizer = function () { - $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize))); + $this.css('font-size', Math.max(Math.min($this.width() / (compressor*fontWidthRate), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize))); }; // Call once to set.