3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.util.TextMetrics"></div>/**
10 * @class Ext.util.TextMetrics
11 * Provides precise pixel measurements for blocks of text so that you can determine exactly how high and
12 * wide, in pixels, a given block of text will be. Note that when measuring text, it should be plain text and
13 * should not contain any HTML, otherwise it may not be measured correctly.
16 Ext.util.TextMetrics = function(){
19 <div id="method-Ext.util.TextMetrics-measure"></div>/**
20 * Measures the size of the specified text
21 * @param {String/HTMLElement} el The element, dom node or id from which to copy existing CSS styles
22 * that can affect the size of the rendered text
23 * @param {String} text The text to measure
24 * @param {Number} fixedWidth (optional) If the text will be multiline, you have to set a fixed width
25 * in order to accurately measure the text height
26 * @return {Object} An object containing the text's size {width: (width), height: (height)}
28 measure : function(el, text, fixedWidth){
30 shared = Ext.util.TextMetrics.Instance(el, fixedWidth);
33 shared.setFixedWidth(fixedWidth || 'auto');
34 return shared.getSize(text);
37 <div id="method-Ext.util.TextMetrics-createInstance"></div>/**
38 * Return a unique TextMetrics instance that can be bound directly to an element and reused. This reduces
39 * the overhead of multiple calls to initialize the style properties on each measurement.
40 * @param {String/HTMLElement} el The element, dom node or id that the instance will be bound to
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 {Ext.util.TextMetrics.Instance} instance The new instance
45 createInstance : function(el, fixedWidth){
46 return Ext.util.TextMetrics.Instance(el, fixedWidth);
51 Ext.util.TextMetrics.Instance = function(bindTo, fixedWidth){
52 var ml = new Ext.Element(document.createElement('div'));
53 document.body.appendChild(ml.dom);
54 ml.position('absolute');
55 ml.setLeftTop(-1000, -1000);
59 ml.setWidth(fixedWidth);
63 <div id="method-Ext.util.TextMetrics-getSize"></div>/**
64 * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
65 * Returns the size of the specified text based on the internal element's style and width properties
66 * @param {String} text The text to measure
67 * @return {Object} An object containing the text's size {width: (width), height: (height)}
69 getSize : function(text){
76 <div id="method-Ext.util.TextMetrics-bind"></div>/**
77 * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
78 * Binds this TextMetrics instance to an element from which to copy existing CSS styles
79 * that can affect the size of the rendered text
80 * @param {String/HTMLElement} el The element, dom node or id
84 Ext.fly(el).getStyles('font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing')
88 <div id="method-Ext.util.TextMetrics-setFixedWidth"></div>/**
89 * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
90 * Sets a fixed width on the internal measurement element. If the text will be multiline, you have
91 * to set a fixed width in order to accurately measure the text height.
92 * @param {Number} width The width to set on the element
94 setFixedWidth : function(width){
98 <div id="method-Ext.util.TextMetrics-getWidth"></div>/**
99 * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
100 * Returns the measured width of the specified text
101 * @param {String} text The text to measure
102 * @return {Number} width The width in pixels
104 getWidth : function(text){
105 ml.dom.style.width = 'auto';
106 return this.getSize(text).width;
109 <div id="method-Ext.util.TextMetrics-getHeight"></div>/**
110 * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
111 * Returns the measured height of the specified text. For multiline text, be sure to call
112 * {@link #setFixedWidth} if necessary.
113 * @param {String} text The text to measure
114 * @return {Number} height The height in pixels
116 getHeight : function(text){
117 return this.getSize(text).height;
121 instance.bind(bindTo);
126 Ext.Element.addMethods({
127 <div id="method-Ext.Element-getTextWidth"></div>/**
128 * Returns the width in pixels of the passed text, or the width of the text in this Element.
129 * @param {String} text The text to measure. Defaults to the innerHTML of the element.
130 * @param {Number} min (Optional) The minumum value to return.
131 * @param {Number} max (Optional) The maximum value to return.
132 * @return {Number} The text width in pixels.
133 * @member Ext.Element getTextWidth
135 getTextWidth : function(text, min, max){
136 return (Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width).constrain(min || 0, max || 1000000);