forked from STRML/textFit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textFit.min.js
14 lines (14 loc) · 3.24 KB
/
textFit.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* textFit v2.1.0
* Previously known as jQuery.textFit
* 8/2013 by STRML (strml.github.com)
* MIT License
*
* To use: textFit(document.getElementById('target-div'), options);
*
* Will make the *text* content inside a container scale to fit the container
* The container is required to have a set width and height
* Uses binary search to fit text with minimal layout calls.
* Version 2.0 does not use jQuery.
*/
(function(e,t){"use strict";if(typeof define==="function"&&define.amd){define([],t)}else{e.textFit=t()}})(typeof global==="object"?global:this,function(){"use strict";return function(t,n){function u(e){if(!l(e)||!r.reProcess&&e.getAttribute("textFitted")){return false}if(!r.reProcess){e.setAttribute("textFitted",1)}var t,n,i,s;var o,u,p;i=e.innerHTML;s=f(e);n=a(e);if(!s||!r.widthOnly&&!n){if(window.console&&!r.suppressErrors){if(!r.widthOnly)console.info("Set a static height and width on the target element "+e.outerHTML+" before using textFit!");else console.info("Set a static width on the target element "+e.outerHTML+" before using textFit!")}return false}if(i.indexOf("textFitted")===-1){t=document.createElement("span");t.className="textFitted";t.style["display"]="inline-block";t.innerHTML=i;e.innerHTML="";e.appendChild(t)}else{t=e.querySelector("span.textFitted");if(c(t,"textFitAlignVert")){t.className=t.className.replace("textFitAlignVert","")}}if(r.alignHoriz){e.style["text-align"]="center";t.style["text-align"]="center"}var d=r.multiLine;if(r.detectMultiLine&&!d&&t.offsetHeight>=parseInt(window.getComputedStyle(t)["font-size"],10)*2){d=true}if(!d){e.style["white-space"]="nowrap"}o=r.minFontSize+1;p=r.maxFontSize+1;while(o<=p){u=parseInt((o+p)/2,10);t.style.fontSize=u+"px";if(t.offsetWidth<=s&&(r.widthOnly||t.offsetHeight<=n)){o=u+1}else{p=u-1}}t.style.fontSize=u-1+"px";if(r.alignVert){h();var v=t.offsetHeight;if(window.getComputedStyle(e)["position"]==="static"){e.style["position"]="relative"}if(!c(t,"textFitAlignVert")){t.className=t.className+" textFitAlignVert"}t.style["height"]=v+"px"}}function a(e){var t=window.getComputedStyle(e,null);return e.clientHeight-parseInt(t.getPropertyValue("padding-top"),10)-parseInt(t.getPropertyValue("padding-bottom"),10)}function f(e){var t=window.getComputedStyle(e,null);return e.clientWidth-parseInt(t.getPropertyValue("padding-left"),10)-parseInt(t.getPropertyValue("padding-right"),10)}function l(e){return typeof HTMLElement==="object"?e instanceof HTMLElement:e&&typeof e==="object"&&e!==null&&e.nodeType===1&&typeof e.nodeName==="string"}function c(e,t){return(" "+e.className+" ").indexOf(" "+t+" ")>-1}function h(){if(document.getElementById("textFitStyleSheet"))return;var e=[".textFitAlignVert{","position: absolute;","top: 0; right: 0; bottom: 0; left: 0;","margin: auto;","}"].join("");var t=document.createElement("style");t.type="text/css";t.id="textFitStyleSheet";t.innerHTML=e;document.body.appendChild(t)}var r={alignVert:false,alignHoriz:false,multiLine:false,detectMultiLine:true,minFontSize:6,maxFontSize:80,reProcess:true,widthOnly:false,suppressErrors:false};for(var i in n){if(n.hasOwnProperty(i)){r[i]=n[i]}}if(typeof t.toArray==="function"){t=t.toArray()}var s=Object.prototype.toString.call(t);if(s!=="[object Array]"&&s!=="[object NodeList]"){t=[t]}for(var o=0;o<t.length;o++){u(t[o])}}})