adaptable-text
Advanced tools
| /* | ||
| * Adaptable Text - v0.1.0 | ||
| * Adaptable Text - v0.1.2 | ||
| * | ||
| * Copyright (c) Damien Seguin - https://github.com/dmnsgn | ||
| * | ||
| * Adapt font size to fit text in element. | ||
| * Adapt font size to fit text in element (WIP) | ||
| * | ||
@@ -54,6 +54,10 @@ * MIT | ||
| var textWidth; | ||
| this.text = this.element.value; | ||
| this.text = this.element.value || this.element.textContent; | ||
| textWidth = this._getTextWidth(this.text, this.styles.fontStyle + " " + this.currentFontSize + "px " + this.styles.fontFamily); | ||
| this.previousFontSize = this.currentFontSize; | ||
| this._checkSize(); | ||
| this.element.style.fontSize = this.currentFontSize + "px"; | ||
| if (this.previousFontSize !== this.currentFontSize) { | ||
| this.currentFontSize = ~~(this.currentFontSize * 100) / 100; | ||
| this.element.style.fontSize = this.currentFontSize + "px"; | ||
| } | ||
| }; | ||
@@ -112,5 +116,5 @@ | ||
| AdaptableText.prototype._calculateMaxCharWidth = function(text, font) { | ||
| AdaptableText.prototype._calculateMaxCharWidth = function() { | ||
| var char, charWidth, chars, len; | ||
| chars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'.split(''); | ||
| chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789!?*()@£$%^&_-+=[]{}:;\'"\\|<>,./~`'.split(''); | ||
| len = chars.length; | ||
@@ -117,0 +121,0 @@ while (len--) { |
| /* | ||
| * Adaptable Text - v0.1.0 | ||
| * Adaptable Text - v0.1.2 | ||
| * | ||
| * Copyright (c) Damien Seguin - https://github.com/dmnsgn | ||
| * | ||
| * Adapt font size to fit text in element. | ||
| * Adapt font size to fit text in element (WIP) | ||
| * | ||
| * MIT | ||
| */ | ||
| (function(){!function(a,b,c){var d,e,f;return f="adaptableText",e={minFontSize:15},d=function(){function d(b,c){this.element=b,this.settings=a.extend({},e,c),this._defaults=e,this._name=f,this.init()}return d.prototype.init=function(){this.reset(),this.adapt()},d.prototype.reset=function(){this.elementWidth=this.element.offsetWidth,this.styles=b.getComputedStyle(this.element),this.currentFontSize=+this.styles.fontSize.replace("px",""),this.initialFontsize=this.currentFontSize,this.maxCharWidth=0,this._calculateMaxCharWidth()},d.prototype.adapt=function(){var a;this.text=this.element.value,a=this._getTextWidth(this.text,this.styles.fontStyle+" "+this.currentFontSize+"px "+this.styles.fontFamily),this._checkSize(),this.element.style.fontSize=this.currentFontSize+"px"},d.prototype._checkSize=function(){var a;(a=this._getTextWidth(this.text,this.styles.fontStyle+" "+this.currentFontSize+"px "+this.styles.fontFamily)<this.elementWidth-this.maxCharWidth?function(b){return function(){var c;return c=b._getTextWidth(b.text,b.styles.fontStyle+" "+b.currentFontSize+"px "+b.styles.fontFamily),b._getTextWidth(b.text,c)<b.elementWidth-b.maxCharWidth&&b.currentFontSize<b.initialFontsize?(b.currentFontSize+=.1,a()):void 0}}(this):function(b){return function(){var c;return c=b._getTextWidth(b.text,b.styles.fontStyle+" "+b.currentFontSize+"px "+b.styles.fontFamily),b._getTextWidth(b.text,c)>b.elementWidth-b.maxCharWidth&&b.currentFontSize>b.settings.minFontSize?(b.currentFontSize-=.1,a()):void 0}}(this))()},d.prototype._getTextWidth=function(a,b){var d,e,f;return d=this.canvas||(this.canvas=c.createElement("canvas")),e=d.getContext("2d"),e.font=b,f=e.measureText(a),f.width},d.prototype._calculateMaxCharWidth=function(){var a,b,c,d;for(c="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM".split(""),d=c.length;d--;)a=c[d],b=this._getTextWidth(a,this.styles.fontStyle+" "+this.styles.fontSize+" "+this.styles.fontFamily),b>this.maxCharWidth&&(this.maxCharWidth=b)},d}(),a.fn[f]=function(b){return this.each(function(){return a.data(this,""+f)?void 0:a.data(this,""+f,new d(this,b))})}}(jQuery,window,document)}).call(this); | ||
| (function(){!function(a,b,c){var d,e,f;return f="adaptableText",e={minFontSize:15},d=function(){function d(b,c){this.element=b,this.settings=a.extend({},e,c),this._defaults=e,this._name=f,this.init()}return d.prototype.init=function(){this.reset(),this.adapt()},d.prototype.reset=function(){this.elementWidth=this.element.offsetWidth,this.styles=b.getComputedStyle(this.element),this.currentFontSize=+this.styles.fontSize.replace("px",""),this.initialFontsize=this.currentFontSize,this.maxCharWidth=0,this._calculateMaxCharWidth()},d.prototype.adapt=function(){var a;this.text=this.element.value||this.element.textContent,a=this._getTextWidth(this.text,this.styles.fontStyle+" "+this.currentFontSize+"px "+this.styles.fontFamily),this.previousFontSize=this.currentFontSize,this._checkSize(),this.previousFontSize!==this.currentFontSize&&(this.currentFontSize=~~(100*this.currentFontSize)/100,this.element.style.fontSize=this.currentFontSize+"px")},d.prototype._checkSize=function(){var a;(a=this._getTextWidth(this.text,this.styles.fontStyle+" "+this.currentFontSize+"px "+this.styles.fontFamily)<this.elementWidth-this.maxCharWidth?function(b){return function(){var c;return c=b._getTextWidth(b.text,b.styles.fontStyle+" "+b.currentFontSize+"px "+b.styles.fontFamily),b._getTextWidth(b.text,c)<b.elementWidth-b.maxCharWidth&&b.currentFontSize<b.initialFontsize?(b.currentFontSize+=.1,a()):void 0}}(this):function(b){return function(){var c;return c=b._getTextWidth(b.text,b.styles.fontStyle+" "+b.currentFontSize+"px "+b.styles.fontFamily),b._getTextWidth(b.text,c)>b.elementWidth-b.maxCharWidth&&b.currentFontSize>b.settings.minFontSize?(b.currentFontSize-=.1,a()):void 0}}(this))()},d.prototype._getTextWidth=function(a,b){var d,e,f;return d=this.canvas||(this.canvas=c.createElement("canvas")),e=d.getContext("2d"),e.font=b,f=e.measureText(a),f.width},d.prototype._calculateMaxCharWidth=function(){var a,b,c,d;for(c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789!?*()@£$%^&_-+=[]{}:;'\"\\|<>,./~`".split(""),d=c.length;d--;)a=c[d],b=this._getTextWidth(a,this.styles.fontStyle+" "+this.styles.fontSize+" "+this.styles.fontFamily),b>this.maxCharWidth&&(this.maxCharWidth=b)},d}(),a.fn[f]=function(b){return this.each(function(){return a.data(this,""+f)?void 0:a.data(this,""+f,new d(this,b))})}}(jQuery,window,document)}).call(this); |
+1
-1
| { | ||
| "name": "adaptable-text", | ||
| "title": "Adaptable Text", | ||
| "version": "0.1.1", | ||
| "version": "0.1.2", | ||
| "description": "Adapt font size to fit text in element (WIP)", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
20991
2.37%310
1.31%