I have a page that shows the company name of the user. the company could have a very long name or a short name. both should automaticly change font-size so text would perfectly fit inside the element:
<h1 class="sidebar-compName responsiveText">{{user.companyName}}123456789</h1>
(the 123456789 is to test if it works and the h1 element has a 200px width) this is how it looks: https://i.stack.imgur.com/QztoT.jpg
i created a system which works when you have 1 text that should be responsive. (because its a while loop it will crash the browser when it has to loop to many times)
//angularjs function which gets called when all content is loaded
$scope.adjustFontsSize = function(value){
//get all elements with the class "responsiveText"
var elems = document.getElementsByClassName("responsiveText");
//loop through all elems
for(var i = 0; i < elems.length; i++){
//set standard size to 50px
var size = 50;
elems[i].style.fontSize = size + "px";
//resize untill it fits within the element (the -75 increases the size a little because it looks small without it)
while (elems[i].scrollWidth -75 > elems[i].offsetWidth) {
size--;
elems[i].style.fontSize = size + "px";
}
}
}
this can only decrease the font size. thats why it needs a standard size.
with my system it looks like this: https://i.stack.imgur.com/2XKVf.jpg
now i want this to be done for multiple elements (with the class "responsiveText") and done by increasing and decreasing the font-size to make it fit perfectly. but i cant seem to figure out how.
it would also be very nice to have a offset with it. so i give it the class "rt1" which fits the text as large as possible inside the element and "rt2" should fit the text but a little bit smaller than the "rt1" inside the element.
EDIT: this is not a duplicate of Dynamically resize font size to fit container size
i already have the "while" possibility but this wont work when i have to many elements that it has to change.