Flex中如何通过hasGlyph()检测是否嵌入特殊字符的例子
By Minidxer | March 22, 2009
接下来的例子演示了Flex中如何通过hasGlyph()检测是否嵌入特殊字符。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="horizontal"
- verticalAlign="top"
- backgroundColor="white"
- initialize="init();">
- <mx:Style source="fonts.css" />
- <mx:Script>
- <![CDATA[
- import mx.events.ListEvent;
- private const START:uint = 0x0020;
- private const END:uint = 0x007E;
- private function init():void {
- arr = Font.enumerateFonts(false)
- arrColl.source = arr;
- var str:String = "";
- var idx:uint;
- for (idx = START; idx < END; idx++) {
- str += String.fromCharCode(idx);
- }
- textArea.text = str;
- }
- private function arrColl_filterFunc(item:Font):Boolean {
- return item.hasGlyphs(textInput.text);
- }
- private function list_rollOut(evt:MouseEvent):void {
- var f:Font = List(evt.currentTarget).selectedItem as Font;
- if (!f) {
- // No selected item, abort!
- return;
- }
- textArea.setStyle("fontFamily", f.fontName);
- samplePanel.title = "Sample: " + f.fontName;
- }
- private function list_itemClick(evt:ListEvent):void {
- var f:Font = List(evt.currentTarget).selectedItem as Font;
- if (!f) {
- // No selected item, abort!
- return;
- }
- textArea.setStyle("fontFamily", f.fontName);
- samplePanel.title = "Sample: " + f.fontName;
- }
- private function list_rollOver(evt:ListEvent):void {
- var f:Font = evt.itemRenderer.data as Font;
- textArea.setStyle("fontFamily", f.fontName);
- samplePanel.title = "Sample: " + f.fontName;
- }
- ]]>
- </mx:Script>
- <mx:Array id="arr" />
- <mx:ArrayCollection id="arrColl"
- filterFunction="arrColl_filterFunc">
- <mx:sort>
- <mx:Sort>
- <mx:SortField name="fontName"
- caseInsensitive="true" />
- </mx:Sort>
- </mx:sort>
- </mx:ArrayCollection>
- <mx:Panel id="panel"
- title="Fonts:"
- status="{arrColl.length}/{arr.length}">
- <mx:List id="list"
- dataProvider="{arrColl}"
- labelField="fontName"
- width="100%"
- rowCount="{arr.length}"
- itemClick="list_itemClick(event);"
- itemRollOver="list_rollOver(event);"
- rollOut="list_rollOut(event);">
- <mx:itemRenderer>
- <mx:Component>
- <mx:Label fontFamily="{data.fontName}" />
- </mx:Component>
- </mx:itemRenderer>
- </mx:List>
- <mx:ControlBar>
- <mx:Label text="Chars:" />
- <mx:TextInput id="textInput"
- width="90"
- change="arrColl.refresh();" />
- </mx:ControlBar>
- </mx:Panel>
- <mx:Panel id="samplePanel"
- title="Sample:"
- width="100%"
- height="100%">
- <mx:TextArea id="textArea"
- fontSize="{slider.value}"
- color="black"
- letterSpacing="10"
- width="100%"
- height="100%" />
- <mx:ControlBar>
- <mx:Label text="Font size:" />
- <mx:HSlider id="slider"
- minimum="10"
- maximum="60"
- value="20"
- snapInterval="1"
- tickInterval="4"
- liveDragging="true"
- width="100%" />
- </mx:ControlBar>
- </mx:Panel>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Other |
No Comments » |
Tags: enumerateFonts, Font, hasGlyphs()