您的位置:首页 > 其它

Flex 查找字符高亮显示

2013-09-15 13:01 225 查看
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"  minWidth="955" minHeight="600">

<fx:Script>
<![CDATA[
import flash.text.engine.FontWeight;

import flashx.textLayout.formats.TextLayoutFormat;

import mx.controls.textClasses.TextRange;
protected function button1_clickHandler(event:MouseEvent):void
{

var tr:TextRange=null;
//清除样式
tr=new TextRange(txt);
tr.color=0x000000;
tr.fontWeight=FontWeight.NORMAL;

var arr:Array=findStr(txt.text,txtFind.text);
var ilen:int=txtFind.text.length;

for (var i:int =0; i <arr.length; i++)
{
tr=new TextRange(txt,false,arr[i],arr[i]+ilen);
tr.color=0xFF0000;
tr.fontWeight=FontWeight.BOLD;
}

}

private function findStr(source:String,str:String):Array
{
var i:int=-1;
var arr:Array=[];
while((i = source.indexOf(str, i + 1)) != -1) {
arr.push(i);
}

return arr;
}

protected function button2_clickHandler(event:MouseEvent):void
{
re.text=txt.text;
var textLayoutFormat:TextLayoutFormat =null;
var arr:Array=findStr(re.text,txtFind.text);
var ilen:int=txtFind.text.length;

textLayoutFormat= new TextLayoutFormat();
textLayoutFormat.fontWeight=flash.text.engine.FontWeight.BOLD;
textLayoutFormat.fontSize = 13;
textLayoutFormat.color = 0xFF0000;

for (var i:int =0; i <arr.length; i++)
{
//re.selectRange(arr[i],arr[i]+ilen);
//re.setFocus();

re.setFormatOfRange(textLayoutFormat,arr[i],arr[i]+ilen);

}
}

]]>
</fx:Script>

<mx:TextArea id="txt" x="124" y="70" width="262" height="259"
text="九点十分了坚实的是到付件电视里就是地方了坚实的,
见到三氟氯菊酸飞机历史积分飞机是打飞机"/>

<s:RichEditableText id="re" x="419" y="39" width="432" height="272"
selectionHighlighting="always" focusedTextSelectionColor="#FF0000"/>

<s:Button x="288" y="337" label="按钮" click="button1_clickHandler(event)"/>
<s:Button x="419" y="341" label="按钮" click="button2_clickHandler(event)"/>
<s:TextInput id="txtFind" x="230" y="27" text="飞机"/>

</s:Application>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: