Skip to content

Instantly share code, notes, and snippets.

@kenkeiter
Created July 12, 2012 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kenkeiter/3101505 to your computer and use it in GitHub Desktop.
Save kenkeiter/3101505 to your computer and use it in GitHub Desktop.
Resize text to fit a min/max.
;(function($){
$.fn.textExtents = function(options){
var spans = $('span:visible', this);
var heightMax = parseInt($(this).css('max-height'));
var widthMax = parseInt($(this).css('max-width'));
var fontSize = parseInt($(this).css('font-size')); /* max font size */
do{
var textHeight = 0;
var textWidth = 0;
$(spans).each(function(){
$(this).css('font-size', fontSize);
if($(this).height() > textHeight){ textHeight = $(this).height(); }
textWidth = textWidth + $(this).width();
});
fontSize = fontSize - 1;
} while ((textHeight > heightMax || textWidth > widthMax) && fontSize > 3);
return this;
}
})(jQuery);
$(document).ready(function(){
$('.extents').textExtents();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment