TextMetrics.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-TextMetrics'>/**
  19. </span> * Provides precise pixel measurements for blocks of text so that you can determine exactly how high and
  20. * wide, in pixels, a given block of text will be. Note that when measuring text, it should be plain text and
  21. * should not contain any HTML, otherwise it may not be measured correctly.
  22. *
  23. * The measurement works by copying the relevant CSS styles that can affect the font related display,
  24. * then checking the size of an element that is auto-sized. Note that if the text is multi-lined, you must
  25. * provide a **fixed width** when doing the measurement.
  26. *
  27. * If multiple measurements are being done on the same element, you create a new instance to initialize
  28. * to avoid the overhead of copying the styles to the element repeatedly.
  29. */
  30. Ext.define('Ext.util.TextMetrics', {
  31. statics: {
  32. shared: null,
  33. <span id='Ext-util-TextMetrics-static-method-measure'> /**
  34. </span> * Measures the size of the specified text
  35. * @param {String/HTMLElement} el The element, dom node or id from which to copy existing CSS styles
  36. * that can affect the size of the rendered text
  37. * @param {String} text The text to measure
  38. * @param {Number} fixedWidth (optional) If the text will be multiline, you have to set a fixed width
  39. * in order to accurately measure the text height
  40. * @return {Object} An object containing the text's size `{width: (width), height: (height)}`
  41. * @static
  42. */
  43. measure: function(el, text, fixedWidth){
  44. var me = this,
  45. shared = me.shared;
  46. if(!shared){
  47. shared = me.shared = new me(el, fixedWidth);
  48. }
  49. shared.bind(el);
  50. shared.setFixedWidth(fixedWidth || 'auto');
  51. return shared.getSize(text);
  52. },
  53. <span id='Ext-util-TextMetrics-static-method-destroy'> /**
  54. </span> * Destroy the TextMetrics instance created by {@link #measure}.
  55. * @static
  56. */
  57. destroy: function(){
  58. var me = this;
  59. Ext.destroy(me.shared);
  60. me.shared = null;
  61. }
  62. },
  63. <span id='Ext-util-TextMetrics-method-constructor'> /**
  64. </span> * Creates new TextMetrics.
  65. * @param {String/HTMLElement/Ext.Element} bindTo The element or its ID to bind to.
  66. * @param {Number} [fixedWidth] A fixed width to apply to the measuring element.
  67. */
  68. constructor: function(bindTo, fixedWidth){
  69. var measure = this.measure = Ext.getBody().createChild({
  70. cls: Ext.baseCSSPrefix + 'textmetrics'
  71. });
  72. this.el = Ext.get(bindTo);
  73. measure.position('absolute');
  74. measure.setLeftTop(-1000, -1000);
  75. measure.hide();
  76. if (fixedWidth) {
  77. measure.setWidth(fixedWidth);
  78. }
  79. },
  80. <span id='Ext-util-TextMetrics-method-getSize'> /**
  81. </span> * Returns the size of the specified text based on the internal element's style and width properties
  82. * @param {String} text The text to measure
  83. * @return {Object} An object containing the text's size `{width: (width), height: (height)}`
  84. */
  85. getSize: function(text){
  86. var measure = this.measure,
  87. size;
  88. measure.update(text);
  89. size = measure.getSize();
  90. measure.update('');
  91. return size;
  92. },
  93. <span id='Ext-util-TextMetrics-method-bind'> /**
  94. </span> * Binds this TextMetrics instance to a new element
  95. * @param {String/HTMLElement/Ext.Element} el The element or its ID.
  96. */
  97. bind: function(el){
  98. var me = this;
  99. me.el = Ext.get(el);
  100. me.measure.setStyle(
  101. me.el.getStyles('font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing')
  102. );
  103. },
  104. <span id='Ext-util-TextMetrics-method-setFixedWidth'> /**
  105. </span> * Sets a fixed width on the internal measurement element. If the text will be multiline, you have
  106. * to set a fixed width in order to accurately measure the text height.
  107. * @param {Number} width The width to set on the element
  108. */
  109. setFixedWidth : function(width){
  110. this.measure.setWidth(width);
  111. },
  112. <span id='Ext-util-TextMetrics-method-getWidth'> /**
  113. </span> * Returns the measured width of the specified text
  114. * @param {String} text The text to measure
  115. * @return {Number} width The width in pixels
  116. */
  117. getWidth : function(text){
  118. this.measure.dom.style.width = 'auto';
  119. return this.getSize(text).width;
  120. },
  121. <span id='Ext-util-TextMetrics-method-getHeight'> /**
  122. </span> * Returns the measured height of the specified text
  123. * @param {String} text The text to measure
  124. * @return {Number} height The height in pixels
  125. */
  126. getHeight : function(text){
  127. return this.getSize(text).height;
  128. },
  129. <span id='Ext-util-TextMetrics-method-destroy'> /**
  130. </span> * Destroy this instance
  131. */
  132. destroy: function(){
  133. var me = this;
  134. me.measure.remove();
  135. delete me.el;
  136. delete me.measure;
  137. }
  138. }, function(){
  139. Ext.Element.addMethods({
  140. <span id='Ext-dom-Element-method-getTextWidth'> /**
  141. </span> * Returns the width in pixels of the passed text, or the width of the text in this Element.
  142. * @param {String} text The text to measure. Defaults to the innerHTML of the element.
  143. * @param {Number} [min] The minumum value to return.
  144. * @param {Number} [max] The maximum value to return.
  145. * @return {Number} The text width in pixels.
  146. * @member Ext.dom.Element
  147. */
  148. getTextWidth : function(text, min, max){
  149. return Ext.Number.constrain(Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width, min || 0, max || 1000000);
  150. }
  151. });
  152. });
  153. </pre>
  154. </body>
  155. </html>