So I am working on a function that resizes font size so the text fills as much of the container as possible across multiple lines without overflow. For instance,
I want the text to cover as much of the 1200px of area within the div without overflowing as possible. This script does this on resize but not on load.
$(document).ready(textFill);
$(window).resize(textFill);
function textFill() {
$(".textFill").each(function() {
var
$text = $(this),
$parent = $text.parent();
var
textW = $text.width(),
textH = $text.height(),
parentW = $parent.width(),
parentH = $parent.height(),
ratio = (parentW + parentH) / (textW + textH);
var
originalSize = parseFloat($text.css('font-size')),
newSize = originalSize * (.9 * ratio);
$text.css("font-size", newSize);
});
}
#container {
width: 400px;
height: 300px;
border: 1px solid blue;
}
.textFill {
display: inline;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<h1 class="textFill">Make this fit as large as possible.</h1>
</div>
When I resize, it fills the container, but onload it does not seem to fire.
EDIT: The function does fire upon document ready, however it does not account for the fully loaded dimensions of the parent. Instead it measures the parents height as the height of the text.
Additionally, I am not sure if the resize event is the one I'd like to use, since it should only adjust font size, maybe I should only execute on change of parent dimensions, is that possible?
Here is the fiddle..