ExtJs源码分析与学习—工具类Ext.util.TextMetrics

 Ext提供了一个很有用的工具类Ext.util.TextMetrics,利用该类可以很方便的为一段文字提供一个精确象素级的测量,以便可以得到某段文字的高度和宽度。该类的实现采用了单例模式,即当调用该类时,该类内部属性shared已实例,下次调用时不需再实例化。先看函数的定义

 

Js代码   收藏代码
  1. Ext.util.TextMetrics = function(){  
  2.     var shared;  
  3.     return {  
  4.         measure : function(el, text, fixedWidth){  
  5.             if(!shared){  
  6.                 shared = Ext.util.TextMetrics.Instance(el, fixedWidth);  
  7.             }  
  8.             shared.bind(el);  
  9.             shared.setFixedWidth(fixedWidth || 'auto');  
  10.             return shared.getSize(text);  
  11.         },  
  12.   
  13.         createInstance : function(el, fixedWidth){  
  14.             return Ext.util.TextMetrics.Instance(el, fixedWidth);  
  15.         }  
  16.     };  
  17. }();  

 

      该函数是自执行函数,执行完后该类提供了两个方法measure和createInstance,分别用来测量元素el的大小和创建实例。
      方法measure中三个参数为el测试的DOM对象,text要测量的文字,fixedWidth设置该值可以根据该值压缩文字的多行显示,从而根据行数的不同返回文本的高度,不设置时会默认根据文本显示的行数返回内容的高度。该方法首先实例化要测量的对象,然后绑定el,绑定的过程会设置其与字体相关的css样式,最后返回一段文字的大小。
上面方法中调用的函数实际上在Ext.util.TextMetrics.Instance中定义,函数主体如下

 

Js代码   收藏代码
  1. Ext.util.TextMetrics.Instance = function(bindTo, fixedWidth){  
  2.     ...  
  3.   
  4.     var instance = {  
  5.       ...  
  6.     };  
  7.   
  8.     instance.bind(bindTo);  
  9.   
  10.     return instance;  
  11. };  

 

      该函数实际上返回闭包变量instance,Ext.util.TextMetrics.Instance中封装了要实现的函数,首先看Instance中的变量

 

Js代码   收藏代码
  1. var ml = new Ext.Element(document.createElement('div'));  
  2.     document.body.appendChild(ml.dom);  
  3.     ml.position('absolute');  
  4.     ml.setLeftTop(-1000, -1000);  
  5.     ml.hide();  
  6.   
  7.     if(fixedWidth){  
  8.         ml.setWidth(fixedWidth);  
  9. }  

    该类定义了辅助div,隐藏显示,用来存放要测试的文字,下面看闭包变量instance中的函数

 

Js代码   收藏代码
  1. /** 
  2.  * 返回一个指定文字的尺寸。该文字内置元素的样式和宽度属性 
  3.  * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p> 
  4.  * Returns the size of the specified text based on the internal element's style and width properties 
  5.  * @param {String} text 要测量的文字 The text to measure  
  6.  * @return {Object} An object containing the text's size {width: (width), height: (height)} 
  7.  */  
  8. getSize : function(text){  
  9.     ml.update(text);  
  10.     var s = ml.getSize();  
  11.     ml.update('');  
  12.     return s;  
  13. },  

 注意返回大小后把ml内容清空

 

Js代码   收藏代码
  1. /** 
  2.   * 绑定某个样式的TextMetrics实例,使得被渲染之文字重新获得CSS样式。 
  3.   * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p> 
  4.   * Binds this TextMetrics instance to an element from which to copy existing CSS styles 
  5.   * that can affect the size of the rendered text 
  6.   * @param {String/HTMLElement} el The element, dom node or id 
  7.   */  
  8.  bind : function(el){  
  9.      ml.setStyle(  
  10.          Ext.fly(el).getStyles('font-size','font-style''font-weight''font-family','line-height''text-transform''letter-spacing')  
  11.      );  
  12.  },  

 绑定时只设置与字体相关的属性

 

Js代码   收藏代码
  1. /** 
  2.  * 对内置的测量元素设置一个固定的宽度。 
  3.  * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p> 
  4.  * Sets a fixed width on the internal measurement element.  If the text will be multiline, you have 
  5.  * to set a fixed width in order to accurately measure the text height. 
  6.  * @param {Number} width The width to set on the element 
  7.  */  
  8. setFixedWidth : function(width){  
  9.     ml.setWidth(width);  
  10.   
  11.   
  12. /** 
  13.  * 返回指定文字的宽度 
  14.  * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p> 
  15.  * Returns the measured width of the specified text 
  16.  * @param {String} text The text to measure 
  17.  * @return {Number} width The width in pixels 
  18.  */  
  19. getWidth : function(text){  
  20.     ml.dom.style.width = 'auto';  
  21.     return this.getSize(text).width;  
  22. },  
  23.   
  24. /** 
  25.  * 返回指定文字的高度,对于多行文本,有可能需要调用 {@link #setFixedWidth} 。 
  26.  * <p><b>Only available on the instance returned from {@link #createInstance}, <u>not</u> on the singleton.</b></p> 
  27.  * Returns the measured height of the specified text.  For multiline text, be sure to call 
  28.  * {@link #setFixedWidth} if necessary. 
  29.  * @param {String} text The text to measure 
  30.  * @return {Number} height The height in pixels 
  31.  */  
  32. getHeight : function(text){  
  33.     return this.getSize(text).height;  
  34. }  

 

最后为对象Ext.Element提供了一个测量文本宽度的方法

 

Js代码   收藏代码
  1. Ext.Element.addMethods({  
  2.     /** 
  3.      * 返回传入文本的宽度,或者该元素下文本的宽度。用该方法即可 
  4.      * Returns the width in pixels of the passed text, or the width of the text in this Element. 
  5.      * @param {String} 要测试的内容 text The text to measure. Defaults to the innerHTML of the element. 
  6.      * @param {Number} min (Optional) The minumum value to return. 
  7.      * @param {Number} max (Optional) The maximum value to return. 
  8.      * @return {Number} The text width in pixels. 
  9.      * @member Ext.Element getTextWidth 
  10.      */  
  11.     getTextWidth : function(text, min, max){  
  12.         return (Ext.util.TextMetrics.measure(this.dom, Ext.value(text, this.dom.innerHTML, true)).width).constrain(min || 0, max || 1000000);  
  13.     }  
  14. });  

 

下面综合看一个例子

 

Html代码   收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.     <head>  
  4.         <title>Ext.util.TextMetrics测试</title>  
  5.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  6.         <link rel="stylesheet" type="text/css"  
  7.             href="../ext-3.3.1/resources/css/ext-all.css" />  
  8.         <script type="text/javascript"  
  9.             src="../ext-3.3.1/adapter/ext/ext-base-debug.js"></script>  
  10.         <script type="text/javascript"  
  11.             src="../ext-3.3.1/ext-all-debug-w-comments.js"></script>  
  12.         <script type="text/javascript"  
  13.             src="../ext-3.3.1/src/locale/ext-lang-zh_CN.js"></script>  
  14.         <script type="text/javascript" src="../ext-3.3.1/src/debug.js"></script>  
  15.         <script type="text/javascript">  
  16.                 Ext.onReady(function() {  
  17.                     Ext.BLANK_IMAGE_URL = '../ext-3.3.1/resources/images/default/s.gif';  
  18.                     Ext.QuickTips.init();  
  19.                     var textEl = Ext.fly('text');  
  20.                     var textWidth = textEl.getTextWidth();  
  21.                     alert(textWidth);  
  22.                     textEl = Ext.fly('text2');  
  23.                     var size = Ext.util.TextMetrics.measure(textEl, textEl.dom.innerHTML);  
  24.                     alert('width:'+size.width+' height:'+size.height);  
  25.                     //多行的测试,设置宽度时会压缩内容,再根据压缩后的行数返回其高度  
  26.                     textEl = Ext.fly('text3');  
  27.                     var size = Ext.util.TextMetrics.measure(textEl, textEl.dom.innerHTML,50);  
  28.                     alert('多行的测试 width:'+size.width+' height:'+size.height);  
  29.                       
  30.                     //返回一个唯一的TextMetrics实例,直接绑定到某个元素和复用,  
  31.                                         //这样会减少在每个测量上初始样式属性的多次调用。  
  32.                     var metrics=Ext.util.TextMetrics.createInstance('text3');  
  33.                     metrics.setFixedWidth(100);  
  34.                     var size=metrics.getSize("中华人民共和国中华人民共和国"  
  35.                                              +"中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国"  
  36.                                              +"中华人民共和国中华人民共和国中华人民共和国中华人民共和国中华人民共和国");  
  37.                     Ext.MessageBox.alert("getsize",String.format("width:{0}px\theight:{1}px",size.width,size.height))  
  38.                 });  
  39.         </script>  
  40.     </head>  
  41.   
  42.     <body>  
  43.     <div id='text'>  
  44.         要测试的文本要测试的文本要测试的文本  
  45.     </div>  
  46.     <div id='text2'>  
  47.         要测试的文本要测试的文本要测试的文本  
  48.     </div>  
  49.     <div id='text3'>  
  50.         要测试的文本要测试的文本要测试的文本  
  51.         要测试的文本要测试的文本要测试的文本  
  52.         要测试的文本要测试的文本要测试的文本  
  53.     </div>  
  54.     </body>  
  55. </html>  

 工具类Ext.util.TextMetrics到此分析完毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值