JavaScript Scale Text to Fit in Fixed Div
This is somewhat of a hack, but will do what you want.
<div id="hidden-resizer" style="visibility: hidden"></div>
Place this at the bottom of your page, where it will not be moving other elements on the page.
Then do this:
var size;
var desired_width = 50;
var resizer = $("#hidden-resizer");
resizer.html("This is the text I want to resize.");
while(resizer.width() > desired_width) {
size = parseInt(resizer.css("font-size"), 10);
resizer.css("font-size", size - 1);
}
$("#target-location").css("font-size", size).html(resizer.html());
scale div content to fit a fixed size
Based on @Doug answer, the implemented solution has been replace the statement:
currentExprFront.innerHTML = currentExpr.innerHTML;
with the statements:
const k = 1.25;
const lk = Math.log(k);
function scaleAndCopy( d, o ) {
/* width and height ratios */
var h = d.offsetHeight/o.offsetHeight;
var v = d.offsetWidth/o.offsetWidth;
/* adjust to 1.25^n */
h = Math.pow(1.25,Math.floor(Math.log(h)/lk));
v = Math.pow(1.25,Math.floor(Math.log(v)/lk));
/* set font size ratio and copy */
var r = 100*Math.min(h,v);
d.style.fontSize = `${r}%`;
d.innerHTML = o.innerHTML
}
scale( currentExprFront, currentExpr );
this solution allows a single-step adjustment and keeps the fontSize stable to minor changes in the content, because the possible font size values are not continuous but 100%*1.25^n: ..., 80%, 100%, 125%, 156%, ... .
Resize text in a fixed size div to fit
You will need some JavaScript to solve this.
This is my example markup
<div style="width: 400px; border: 1px solid black;">
<span style="display: inline-block; font-size: 50px;">This is my text that I want to fit</span>
</div>
I gave the span the display value inline-block
so I could measuer its width.
Then the following jquery based script will make it shink until it's just a little bit too small.
$(function() {
var span = $('span');
var fontSize = parseInt(span.css('font-size'));
do {
fontSize--;
span.css('font-size', fontSize.toString() + 'px');
} while (span.width() >= 400);
});
Check out the JSFiddle for demonstration: http://jsfiddle.net/gEjTz/
Fit text perfectly inside a div (height and width) without affecting the size of the div
Here's the same answer, but in Javascript
var autoSizeText;
autoSizeText = function() {
var el, elements, _i, _len, _results;
elements = $('.resize');
console.log(elements);
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;
};
$(document).ready(function() {
return autoSizeText();
});
By the way...if you ever need to convert coffeescript to javascript, just go to js2coffee.org
Resizing an arbitrary length string to fit into a fixed-size div
One thing here you can do is get the label text into similar size of the div with following CSS attributes and then count it's scrollwidth, then write a javascript loop and decrease font-size by some pixels till you find it's scrollwidth is less than your desired width.
white-space: pre;
overflow-x: scroll;
Small Javascript code which I tried
// dummy divelement
var divElement = document.getElementById('#myelement')
var fontSize = 15; // Starting from 15
while(divElement.offsetWidth < divElement.scrollWidth) {
divElement.style.fontSize = fontSize + "px"
fontSize--;
}
Does this answer your question? or should I provide you whole example?
Related Topics
How to Tell If Caps Lock Is on Using JavaScript
Remove All Elements Contained in Another Array
Add a "Hook" to All Ajax Requests on a Page
Array VS. Object Efficiency in JavaScript
How to Get a Word Under Cursor Using JavaScript
Jquery Check If Element Is Visible in Viewport
Comparing Arrays of Objects in JavaScript
Parse Date Without Timezone JavaScript
How Does the New Operator Work in JavaScript
Settimeout and "This" in JavaScript
Script Tag in JavaScript String
How to Dodge Jquery Promises Completely When Chaining Two Async Jquery Functions
Alternation Operator Inside Square Brackets Does Not Work
How to Decode Jwt Token in JavaScript Without Using a Library
React-Router - Pass Props to Handler Component
How to Call Reduce on an Array of Objects to Sum Their Properties