Flex中如何通过hasGlyph()检测是否嵌入特殊字符的例子

By Minidxer | March 22, 2009

接下来的例子演示了Flex中如何通过hasGlyph()检测是否嵌入特殊字符。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="horizontal"
  4.         verticalAlign="top"
  5.         backgroundColor="white"
  6.         initialize="init();">
  7.  
  8.     <mx:Style source="fonts.css" />
  9.  
  10.     <mx:Script>
  11.         <![CDATA[
  12.             import mx.events.ListEvent;
  13.  
  14.             private const START:uint = 0x0020;
  15.             private const END:uint = 0x007E;
  16.  
  17.             private function init():void {
  18.                 arr = Font.enumerateFonts(false)
  19.                 arrColl.source = arr;
  20.  
  21.                 var str:String = "";
  22.                 var idx:uint;
  23.                 for (idx = START; idx < END; idx++) {
  24.                     str += String.fromCharCode(idx);
  25.                 }
  26.                 textArea.text = str;
  27.             }
  28.  
  29.             private function arrColl_filterFunc(item:Font):Boolean {
  30.                 return item.hasGlyphs(textInput.text);
  31.             }
  32.  
  33.             private function list_rollOut(evt:MouseEvent):void {
  34.                 var f:Font = List(evt.currentTarget).selectedItem as Font;
  35.                 if (!f) {
  36.                     // No selected item, abort!
  37.                     return;
  38.                 }
  39.                 textArea.setStyle("fontFamily", f.fontName);
  40.                 samplePanel.title = "Sample: " + f.fontName;
  41.             }
  42.  
  43.             private function list_itemClick(evt:ListEvent):void {
  44.                 var f:Font = List(evt.currentTarget).selectedItem as Font;
  45.                 if (!f) {
  46.                     // No selected item, abort!
  47.                     return;
  48.                 }
  49.                 textArea.setStyle("fontFamily", f.fontName);
  50.                 samplePanel.title = "Sample: " + f.fontName;
  51.             }
  52.  
  53.             private function list_rollOver(evt:ListEvent):void {
  54.                 var f:Font = evt.itemRenderer.data as Font;
  55.                 textArea.setStyle("fontFamily", f.fontName);
  56.                 samplePanel.title = "Sample: " + f.fontName;
  57.             }
  58.         ]]>
  59.     </mx:Script>
  60.  
  61.     <mx:Array id="arr" />
  62.  
  63.     <mx:ArrayCollection id="arrColl"
  64.             filterFunction="arrColl_filterFunc">
  65.         <mx:sort>
  66.             <mx:Sort>
  67.                 <mx:SortField name="fontName"
  68.                         caseInsensitive="true" />
  69.             </mx:Sort>
  70.         </mx:sort>
  71.     </mx:ArrayCollection>
  72.  
  73.     <mx:Panel id="panel"
  74.             title="Fonts:"
  75.             status="{arrColl.length}/{arr.length}">
  76.         <mx:List id="list"
  77.                 dataProvider="{arrColl}"
  78.                 labelField="fontName"
  79.                 width="100%"
  80.                 rowCount="{arr.length}"
  81.                 itemClick="list_itemClick(event);"
  82.                 itemRollOver="list_rollOver(event);"
  83.                 rollOut="list_rollOut(event);">
  84.             <mx:itemRenderer>
  85.                 <mx:Component>
  86.                     <mx:Label fontFamily="{data.fontName}" />
  87.                 </mx:Component>
  88.             </mx:itemRenderer>
  89.         </mx:List>
  90.         <mx:ControlBar>
  91.             <mx:Label text="Chars:" />
  92.             <mx:TextInput id="textInput"
  93.                     width="90"
  94.                     change="arrColl.refresh();" />
  95.         </mx:ControlBar>
  96.     </mx:Panel>
  97.  
  98.     <mx:Panel id="samplePanel"
  99.             title="Sample:"
  100.             width="100%"
  101.             height="100%">
  102.         <mx:TextArea id="textArea"
  103.                 fontSize="{slider.value}"
  104.                 color="black"
  105.                 letterSpacing="10"
  106.                 width="100%"
  107.                 height="100%" />
  108.         <mx:ControlBar>
  109.             <mx:Label text="Font size:" />
  110.             <mx:HSlider id="slider"
  111.                     minimum="10"
  112.                     maximum="60"
  113.                     value="20"
  114.                     snapInterval="1"
  115.                     tickInterval="4"
  116.                     liveDragging="true"
  117.                     width="100%" />
  118.         </mx:ControlBar>
  119.     </mx:Panel>
  120.  
  121. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Other | No Comments » | Tags: , ,

Search Posts