2 * @class Ext.util.TextMetrics
4 * Provides precise pixel measurements for blocks of text so that you can determine exactly how high and
5 * wide, in pixels, a given block of text will be. Note that when measuring text, it should be plain text and
6 * should not contain any HTML, otherwise it may not be measured correctly.</p>
7 * <p>The measurement works by copying the relevant CSS styles that can affect the font related display,
8 * then checking the size of an element that is auto-sized. Note that if the text is multi-lined, you must
9 * provide a <b>fixed width</b> when doing the measurement.</p>
12 * If multiple measurements are being done on the same element, you create a new instance to initialize
13 * to avoid the overhead of copying the styles to the element repeatedly.
16 Ext.define('Ext.util.TextMetrics', {
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){
33 shared = me.shared = new me(el, fixedWidth);
36 shared.setFixedWidth(fixedWidth || 'auto');
37 return shared.getSize(text);
41 * Destroy the TextMetrics instance created by {@link #measure}.
45 Ext.destroy(me.shared);
52 * @param {Mixed} bindTo The element to bind to.
53 * @param {Number} fixedWidth A fixed width to apply to the measuring element.
55 constructor: function(bindTo, fixedWidth){
56 var measure = this.measure = Ext.getBody().createChild({
59 this.el = Ext.get(bindTo);
61 measure.position('absolute');
62 measure.setLeftTop(-1000, -1000);
66 measure.setWidth(fixedWidth);
71 * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p>
72 * Returns the size of the specified text based on the internal element's style and width properties
73 * @param {String} text The text to measure
74 * @return {Object} An object containing the text's size {width: (width), height: (height)}
76 getSize: function(text){
77 var measure = this.measure,
81 size = measure.getSize();
87 * Binds this TextMetrics instance to a new element
88 * @param {Mixed} el The element
95 me.el.getStyles('font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing')
100 * Sets a fixed width on the internal measurement element. If the text will be multiline, you have
101 * to set a fixed width in order to accurately measure the text height.
102 * @param {Number} width The width to set on the element
104 setFixedWidth : function(width){
105 this.measure.setWidth(width);
109 * Returns the measured width of the specified text
110 * @param {String} text The text to measure
111 * @return {Number} width The width in pixels
113 getWidth : function(text){
114 this.measure.dom.style.width = 'auto';
115 return this.getSize(text).width;
119 * Returns the measured height of the specified text
120 * @param {String} text The text to measure
121 * @return {Number} height The height in pixels
123 getHeight : function(text){
124 return this.getSize(text).height;
128 * Destroy this instance
137 Ext.core.Element.addMethods({
139 * Returns the width in pixels of the passed text, or the width of the text in this Element.
140 * @param {String} text The text to measure. Defaults to the innerHTML of the element.
141 * @param {Number} min (Optional) The minumum value to return.
142 * @param {Number} max (Optional) The maximum value to return.
143 * @return {Number} The text width in pixels.
144 * @member Ext.core.Element getTextWidth
146 getTextWidth : function(text, min, max){
147 return Ext.Number.constrain(Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width, min || 0, max || 1000000);