跟我一起学extjs5(06--使用图标字体来美化按钮)

来源:互联网 发布:淘宝售后怎么赚钱 编辑:程序博客网 时间:2024/05/16 17:49

转自:http://blog.csdn.net/jfok/article/details/35994081

跟我一起学extjs5(06--使用图标字体来美化按钮)


        sencha 的例子中,有使用图标字体来美化按钮的例子,这个用起来又方便风格又统一,例如下图:



        上面图标字体的使用方法也很简单,只要下载Font Awesome的css和图标文件,放到项目里就可以了。部分图标截图:


        Font Awesome的网站为:点击打开链接。进入网站后,先下载Font Awesome 3.0,解压缩后,将css和font目录拷贝到war目录下。
(Font Awesome最新版本为4.0,网址为:http://fontawesome.io/ )


        文件拷贝进来以后,需要在index.html中引入css文件。
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5.   
  6. <title>app</title>  
  7.   
  8. <!-- 引入Font Awesome的css文件 -->  
  9. <link type="text/css" rel="stylesheet" href="css/font-awesome.css">  
  10.   
  11. <!-- The line below must be kept intact for Sencha Cmd to build your application -->  
  12. <script id="microloader" type="text/javascript" src="bootstrap.js"></script>  
  13.   
  14. </head>  
  15. <body></body>  
  16. </html>  

        至此准备工作结束,可以字体文件可以使用了。对于一个Button来说,在其文字前面放一个图标可以使用属性icon,iconCls,对于图标字体来说,可以使用iconCls属性,也可以使用glyph这个属性。我们先来看一段该css之中的设置:
[css] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /* Font Awesome uses the Unicode Private Use Area (PUA) to ensure screen 
  2.    readers do not read off random characters that represent icons */  
  3. .icon-glass:before {  
  4.   content"\f000";  
  5. }  
  6. .icon-music:before {  
  7.   content"\f001";  
  8. }  
  9. .icon-search:before {  
  10.   content"\f002";  
  11. }  
  12. .icon-envelope-alt:before {  
  13.   content"\f003";  
  14. }  
        从其css的描述中可以看出,其命名中就表示了该图标的名称,比如说icon-search是一个搜索的图标,在Button使用的时候,可以这样加入属性:

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. {  
  2.                         text : '搜索',  
  3.                         iconCls : 'icon-search'  
  4.                     }, {  
  5.                         text : '设置',  
  6.                         glyph : 0xf0c9  
  7.                     }  
        这二种方式加入的icon会有不同之处:,可以看出来用glyph设置的更好一些。为了找到这个数字,你先要去Font Awesome 网站上找到你需要的图标,记下名称,然后打开 css 目录下的 font-awesome.css,从中找到该名称的.class值,然后记下content的值。现在我们对top和bottom中的相应按钮加入图标字体。
[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. Ext.define('app.view.main.region.Top', {  
  2.   
  3.             extend : 'Ext.toolbar.Toolbar',  
  4.   
  5.             alias : 'widget.maintop'// 定义了这个组件的xtype类型为maintop  
  6.   
  7.             items : [{  
  8.                         xtype : 'image',  
  9.                         bind : { // 数据绑定到MainModel中data的system.iconUrl  
  10.                             hidden : '{!system.iconUrl}'// 如果system.iconUrl未设置,则此image不显示  
  11.                             src : '{system.iconUrl}' // 根据system.iconUrl的设置来加载图片  
  12.                         }  
  13.                     }, {  
  14.                         xtype : 'label',  
  15.                         bind : {  
  16.                             text : '{system.name}' // text值绑定到system.name  
  17.                         },  
  18.                         style : 'font-size : 20px; color : blue;'  
  19.                     }, {  
  20.                         xtype : 'label',  
  21.                         bind : {  
  22.                             text : '{system.version}'  
  23.                         }  
  24.                     }, '->', {  
  25.                         text : '菜单',  
  26.                         glyph : 0xf0c9,  
  27.                         menu : [{  
  28.                                     text : '工程管理',  
  29.                                     menu : [{  
  30.                                                 text : '工程项目'  
  31.                                             }, {  
  32.                                                 text : '工程标段'  
  33.                                             }]  
  34.                                 }]  
  35.                     }, ' '' ', {  
  36.                         text : '主页',  
  37.                         glyph : 0xf015  
  38.                     }, {  
  39.                         text : '帮助',  
  40.                         glyph : 0xf059  
  41.                     }, {  
  42.                         text : '关于',  
  43.                         glyph : 0xf06a  
  44.                     }, {  
  45.                         text : '注销',  
  46.                         glyph : 0xf011  
  47.                     }, '->''->', {  
  48.                         text : '搜索',  
  49.                         glyph : 0xf002  
  50.                     }, {  
  51.                         text : '设置',  
  52.                         glyph : 0xf013  
  53.                     }]  
  54.   
  55.         });  

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. Ext.define('app.view.main.region.Bottom', {  
  2.   
  3.             extend : 'Ext.toolbar.Toolbar',  
  4.   
  5.             alias : 'widget.mainbottom',  
  6.   
  7.             items : [{  
  8.                         bind : {  
  9.                             text : '使用单位:{user.company}'  
  10.                         },  
  11.                         glyph : 0xf0f7  
  12.                     }, {  
  13.                         bind : {  
  14.                             text : '用户:{user.name}'  
  15.                         },  
  16.                         glyph : 0xf007  
  17.                     }, '->', {  
  18.                         bind : {  
  19.                             text : '{service.company}'  
  20.                         },  
  21.                         glyph : 0xf059  
  22.   
  23.                     }, {  
  24.                         bind : {  
  25.                             text : '{service.name}'  
  26.                         }  
  27.                     }, {  
  28.                         bind : {  
  29.                             text : '{service.phonenumber}'  
  30.                         },  
  31.                         glyph : 0xf095  
  32.                     }, {  
  33.                         bind : {  
  34.                             hidden : '{!service.email}'// 绑定值前面加!表示取反,如果有email则不隐藏,如果email未设置,则隐藏  
  35.                             text : 'eMail:{service.email}'  
  36.                         },  
  37.                         glyph : 0xf003  
  38.                     }, {  
  39.                         bind : {  
  40.                             text : '©{service.copyright}'  
  41.                         }  
  42.                     }]  
  43.         });  

        在修改了上面的glyph之后,还不能正确的显示图标,必须指定一下字体才行。修改Main.js,在里面加入初始化函数。

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. initComponent : function() {  
  2.     Ext.setGlyphFontFamily('FontAwesome'); // 设置图标字体文件,只有设置了以后才能用glyph属性  
  3.     this.callParent();  
  4. },  

        经过以上的操作,图标字体就可以正常显示了。具体效果如下:


        细节决定成败,虽然加了图标字体的按钮比较好看,但是最好把外框和背景去掉,只有鼠标移上去的时候才显示。下一节我们继续Button创建一个自定义的Button,让他的背景是透明的。
0 0
原创粉丝点击