* initial import default tip
authorAndrew Gwozdziewycz <hg@apgwoz.com>
Fri Sep 07 21:33:34 2007 -0700 (4 years ago)
changeset 05cfc9923488e
* initial import
textfit.html
textfit.js
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/textfit.html	Fri Sep 07 21:33:34 2007 -0700
     1.3 @@ -0,0 +1,23 @@
     1.4 +<html>
     1.5 +  <head>
     1.6 +     <style type="text/css">
     1.7 +        #scaleto {
     1.8 +           width: 100%;
     1.9 +           height: 100%;
    1.10 +           border: 1px solid red;
    1.11 +        }
    1.12 +     </style>
    1.13 +  </head>
    1.14 +  <body>
    1.15 +     <div id="scaleto"><span>Resize your browser window.</span></div>
    1.16 +       
    1.17 +     <script type="text/javascript" src="http://www.apgwoz.com/code/textfit.js"></script>
    1.18 +     <script type="text/javascript">
    1.19 +        //<![CDATA[
    1.20 +        window.onresize = function(e) {
    1.21 +            textFit(document.getElementById('scaleto').firstChild, 50, 1000, document.getElementById('scaleto').offsetWidth, document.getElementById('scaleto').offsetHeight);
    1.22 +        }
    1.23 +        //]]>
    1.24 +     </script>
    1.25 +  </body>
    1.26 +</html>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/textfit.js	Fri Sep 07 21:33:34 2007 -0700
     2.3 @@ -0,0 +1,27 @@
     2.4 +/**
     2.5 + * (C) 2007 Andrew Gwozdziewycz <apgwoz@gmail.com>
     2.6 + * VERSION: 0.1, Released under an MIT License. 
     2.7 + * URL: http://www.apgwoz.com/textfit/
     2.8 + **/
     2.9 +function textFit(node, minfs, maxfs, width, height) {
    2.10 +    var fits = function(sz) {
    2.11 +        node.style.fontSize = sz + 'px';
    2.12 +        return node.offsetWidth <= width && node.offsetHeight <= height;
    2.13 +    };
    2.14 +    var hfs = maxfs - minfs;
    2.15 +    var tst = minfs + Math.ceil(hfs / 2);
    2.16 +    var opsz = node.style.fontSize;
    2.17 +    
    2.18 +    while ((hfs/2) >= 1) {
    2.19 +        if (fits(tst)) {
    2.20 +            minfs = tst;
    2.21 +        }
    2.22 +        else {
    2.23 +            maxfs = tst;
    2.24 +        }
    2.25 +        hfs = maxfs - minfs;
    2.26 +        tst = minfs + Math.ceil(hfs/2);
    2.27 +    }
    2.28 +    opsz = fits(maxfs) ? maxfs: minfs;
    2.29 +    node.style.fontSize = opsz + 'px';
    2.30 +}