Io uso this plugin che ho fatta sulla base di fitText.js, perché fitText non si adatta alle mie esigenze, perché non conosco la lunghezza delle stringhe da ridimensionare, quindi il parametro di ridimensionamento della correzione di fitText non funziona in tutti i casi.
$.fn.adjustTextSize = function (set_max_size, min_size) {
min_size = min_size || 12; // if no value then set a default one
var string, width, line, initFontSize, returnFontSize, ratio;
return this.each(function() {
// Store the object
var $this = $(this);
var resizer = function() {
string = $this;
string.html('<span style="white-space: nowrap;">' + string.html() + '</span>');
width = string.width();
line = $(string.children('span'));
initFontSize = parseInt(string.css('font-size'));
ratio = width/line.width();
returnFontSize = initFontSize*ratio;
if (set_max_size && returnFontSize > initFontSize) {
returnFontSize = initFontSize;
}
if (min_size && returnFontSize < min_size) {
returnFontSize = min_size;
}
string.css('font-size',returnFontSize);
while (line.width() >= width) {
if (min_size && returnFontSize <= min_size) {
string.html(line.html());
return false;
}
string.css('font-size', --returnFontSize);
}
string.html(line.html());
}
// Call once to set.
resizer();
// Call on resize. Opera debounces their resize by default.
$(window).on('resize orientationchange', resizer);
});
};
$('.js-adjust-text').adjustTextSize(false, 12);
$('.js-adjust-text-limit').adjustTextSize(true, 30);
Questo plugin ottenere due parametri:
- set_max_size: booleane per limitare dimensione massima del carattere alla sua dimensione definita nei CSS
- min_size: numero intero di limitare dimensione minima dei caratteri.
Spero che funzioni per il tuo caso.
fonte
2013-11-21 14:32:55
possibile duplicato di [Javascript che imposta automaticamente font-size sull'elemento modo che il testo non traboccare? (autofit)] (http://stackoverflow.com/questions/1178651/javascript-that-automatically-sets-font-size-on-element-so-that-text-doesnt-ove) – Quentin
Questa è una domanda duplicata qui http://stackoverflow.com/questions/3840452/change-font-size-to-automatically-fit-divs-height-width – coder