4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-TextMetrics'>/**
19 </span> * @class Ext.util.TextMetrics
21 * Provides precise pixel measurements for blocks of text so that you can determine exactly how high and
22 * wide, in pixels, a given block of text will be. Note that when measuring text, it should be plain text and
23 * should not contain any HTML, otherwise it may not be measured correctly.</p>
24 * <p>The measurement works by copying the relevant CSS styles that can affect the font related display,
25 * then checking the size of an element that is auto-sized. Note that if the text is multi-lined, you must
26 * provide a <b>fixed width</b> when doing the measurement.</p>
29 * If multiple measurements are being done on the same element, you create a new instance to initialize
30 * to avoid the overhead of copying the styles to the element repeatedly.
33 Ext.define('Ext.util.TextMetrics', {
36 <span id='Ext-util-TextMetrics-method-measure'> /**
37 </span> * Measures the size of the specified text
38 * @param {String/HTMLElement} el The element, dom node or id from which to copy existing CSS styles
39 * that can affect the size of the rendered text
40 * @param {String} text The text to measure
41 * @param {Number} fixedWidth (optional) If the text will be multiline, you have to set a fixed width
42 * in order to accurately measure the text height
43 * @return {Object} An object containing the text's size {width: (width), height: (height)}
45 measure: function(el, text, fixedWidth){
50 shared = me.shared = new me(el, fixedWidth);
53 shared.setFixedWidth(fixedWidth || 'auto');
54 return shared.getSize(text);
57 <span id='Ext-util-TextMetrics-method-destroy'> /**
58 </span> * Destroy the TextMetrics instance created by {@link #measure}.
62 Ext.destroy(me.shared);
67 <span id='Ext-util-TextMetrics-method-constructor'> /**
68 </span> * @constructor
69 * @param {Mixed} bindTo The element to bind to.
70 * @param {Number} fixedWidth A fixed width to apply to the measuring element.
72 constructor: function(bindTo, fixedWidth){
73 var measure = this.measure = Ext.getBody().createChild({
76 this.el = Ext.get(bindTo);
78 measure.position('absolute');
79 measure.setLeftTop(-1000, -1000);
83 measure.setWidth(fixedWidth);
87 <span id='Ext-util-TextMetrics-method-getSize'> /**
88 </span> * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
89 * Returns the size of the specified text based on the internal element's style and width properties
90 * @param {String} text The text to measure
91 * @return {Object} An object containing the text's size {width: (width), height: (height)}
93 getSize: function(text){
94 var measure = this.measure,
98 size = measure.getSize();
103 <span id='Ext-util-TextMetrics-method-bind'> /**
104 </span> * Binds this TextMetrics instance to a new element
105 * @param {Mixed} el The element
112 me.el.getStyles('font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing')
116 <span id='Ext-util-TextMetrics-method-setFixedWidth'> /**
117 </span> * Sets a fixed width on the internal measurement element. If the text will be multiline, you have
118 * to set a fixed width in order to accurately measure the text height.
119 * @param {Number} width The width to set on the element
121 setFixedWidth : function(width){
122 this.measure.setWidth(width);
125 <span id='Ext-util-TextMetrics-method-getWidth'> /**
126 </span> * Returns the measured width of the specified text
127 * @param {String} text The text to measure
128 * @return {Number} width The width in pixels
130 getWidth : function(text){
131 this.measure.dom.style.width = 'auto';
132 return this.getSize(text).width;
135 <span id='Ext-util-TextMetrics-method-getHeight'> /**
136 </span> * Returns the measured height of the specified text
137 * @param {String} text The text to measure
138 * @return {Number} height The height in pixels
140 getHeight : function(text){
141 return this.getSize(text).height;
144 <span id='Ext-util-TextMetrics-method-destroy'> /**
145 </span> * Destroy this instance
154 Ext.core.Element.addMethods({
155 <span id='Ext-core-Element-method-getTextWidth'> /**
156 </span> * Returns the width in pixels of the passed text, or the width of the text in this Element.
157 * @param {String} text The text to measure. Defaults to the innerHTML of the element.
158 * @param {Number} min (Optional) The minumum value to return.
159 * @param {Number} max (Optional) The maximum value to return.
160 * @return {Number} The text width in pixels.
161 * @member Ext.core.Element getTextWidth
163 getTextWidth : function(text, min, max){
164 return Ext.Number.constrain(Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width, min || 0, max || 1000000);