Skip to content

Instantly share code, notes, and snippets.

@sdiama
Created May 19, 2020 09:15
Show Gist options
  • Save sdiama/562ff47ae588d6d7fcac4c5a9a2d5494 to your computer and use it in GitHub Desktop.
Save sdiama/562ff47ae588d6d7fcac4c5a9a2d5494 to your computer and use it in GitHub Desktop.
Auto size text to fit within a container
/**
* @param {string} inSelector - The selector containing text to auto size.
*/
window.autoSizeText = function(inSelector) {
var el, elements, _i, _len, _results;
elements = $(inSelector);
if (elements.length < 0) {
return;
}
_results = [];
for (_i = 0, _len = elements.length; _i < _len; _i++) {
el = elements[_i];
_results.push((function(el) {
var resizeText, _results1;
resizeText = function() {
var elNewFontSize;
elNewFontSize = (parseInt($(el).css('font-size').slice(0, -2)) - 1) + 'px';
return $(el).css('font-size', elNewFontSize);
};
_results1 = [];
while (el.scrollHeight > el.offsetHeight) {
_results1.push(resizeText());
}
return _results1;
})(el));
}
return _results;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment