Maybe you have read RegExr: Online Regular Expression sample(www.regexr.com) , the high-light effect of matched characters is perfect and with the mouse moving will show the detail information of the alphabetic string as the following image show.
Because it adds a DisplayObject into the matched alphabetic string.
Then let’s try to do this effect.
How to locate postion of the high-light and height and width is the important point. We could get the relative data by the TextField.
1.getLineIndexOfChar(charIndex:int):int
Returns the zero-based index value of the line containing the character specified by the charIndex parameter.
2.getLineOffset(lineIndex:int):int
Returns the character index of the first character in the line that the lineIndex parameter specifies.
3.getLineLength(lineIndex:int):int
Returns the number of characters in a specific text line.
4.getLineIndexOfChar(charIndex:int):int
Returns the zero-based index value of the line containing the character specified by the charIndex parameter.
5.getCharBoundaries(charIndex:int):Rectangle
Returns a rectangle that is the bounding box of the character.
Let’s see another image to understand the structure of flash text.
Pay more attention of the “2-pixel gutter”. If forget adding it to the postion formula, the high light area will be offset by 2 pixels.
Now let’s first consider the position of the high-light area ( the text arranged in this article are limited from left to right )
1. The case which selected text block out of the TextField displaying area could be ignored.
2. Selected text block in the TextField displaying area. Devide the selected text block into “single line”. The first line high-light area is from beginIndex to end characters of the first line. The middle lines high-light area is from the line begin to line end. The last line high-light area is from the line begin to endIndex.

3. Part of the selected text block in the displaying area and part out of the displaying area. Then there are two situation:
1) If the start index value less than the first character of the displaying area then just need to high-light the beginIndex to endIndex as following image.

2) If the end index value bigger than the end character of the displaying area then just need to high-light the beginIndex to endIndex as following image.
Summarized as follows:
1. Get the effective biginIndex and endIndex.
2. The single line do the single line high-light.
3. The multi-line do the high-light according the first line, middle line and last line.
When we have known the basic method, built the the new project “HighlightText”
New built the folder com->kingnare->regex
New built class HighlightBlock under regex folder and superclass choose UIMovieClip.
According the previous idea, we wrote the class Highlight Block:
Then let’s simply explain the process of the HighlightBlock:
Get the begin and end index of the high-light block in displaying area by the way of getValidBeginCharIndex and getValidEndCharIndex. Then high light the block by normalDraw.
In normalDraw method, get the distance which the imported index value located line relative to the text registration point by getDisLineHeightByLine. It will be convenient to calculate the y coordinate of high-light block.
Then how to use the class?
The access parameter type is IUITextField, but the other internal components like TextArea is protected then need to visit by succession. New built file com->components, new built class BlockTextArea and superclass choose TextArea:
The code as below:
- package com.components
- {
- import com.kingnare.regex.HighlightBlock;
- import flash.display.DisplayObject;
- import flash.geom.Point;
- import mx.controls.TextArea;
- public class BlockTextArea extends TextArea
- {
- private var blockArray:Array;
- public function BlockTextArea()
- {
- super();
- blockArray = [];
- }
- public function showBlock(beginIndex:int, endIndex:int):void
- {
- var movieTip:HighlightBlock= new HighlightBlock(this.textField);
- movieTip.offsetPoint = new Point(0, 0);
- movieTip.highLightDraw(beginIndex, endIndex);
- movieTip.toolTip = “beginIndex: “+beginIndex+“\nendIndex: “+endIndex+“\nlength: “+(endIndex-beginIndex+1).toString()+“\ntext:\t”+this.textField.text.substring(beginIndex, endIndex+1);
- clearBlock();
- this.addChild(movieTip);
- blockArray.push(movieTip);
- }
- public function clearBlock():void
- {
- var len:uint = blockArray.length;
- for(var k:uint=0;k<len;k++)
- {
- var obj:DisplayObject = blockArray[k]
- this.removeChild(obj);
- obj = null;
- }
- blockArray = [];
- }
- }
- }
Switch to the HighlightTest.mxml, set the height and width of the main program is 160×420 and new add a component BlockTextArea. Then add other components.
- <components:BlockTextArea id=”lightText” width=”400″ height=”100″ y=”10″ x=”10″/>
- <mx:Button y=”118″ label=”Show Highlight” right=”10″/>
- <mx:TextInput id=”beginIndexInput” x=”92″ y=”118″ width=”60″/>
- <mx:TextInput id=”endIndexInput” x=”232″ y=”118″ width=”60″/>
- <mx:Label x=”10″ y=”121″ text=”beginIndex:”/>
- <mx:Label x=”160″ y=”121″ text=”endIndex:”/>
Switch to the Design panel:
Next add ActionScript code for the main program.
First is the event applicationComplete of Application.
- <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” xmlns:components=”com.components.*”
- width=”420″ height=”160″ applicationComplete=”initApp();”>
Next write the initApp in ActionScript:
- private function initApp():void
- {
- }
In this way we initialize the lightText, beginIndexInput, endIndexInput and add the ScrollEvent’s Listener of lightText.
- import mx.events.ScrollEvent;
- import mx.managers.ToolTipManager;
- private function initApp():void
- {
- //ToolTip display immediately
- ToolTipManager.showDelay = 0;
- lightText.addEventListener(ScrollEvent.SCROLL, scrollEvent);
- lightText.htmlText = “Documentation for classes includes syntax, “ +
- “<b><font size=’18′>usage</font></b> information, and code samples for methods, “ +
- “properties, “ +
- “and event handlers and listeners for those APIs that belong to a specific class “ +
- “in ActionScript (as opposed to global functions or properties). “ +
- “The <font size=’24′>classes</font> are listed alphabetically. “ +
- “If you are not sure to which class a certain method or property belongs, “ +
- “you can look it up in the Index.”;
- beginIndexInput.text = “43″;
- endIndexInput.text = “300″;
- }
Complete the click way of “show highlight” button as the way of high-light displaying.
- private function showBlock():void
- {
- lightText.showBlock(parseInt(beginIndexInput.text), parseInt(endIndexInput.text));
- }
In order to update the high-light block immediately, we need to add a timer. When the text changed, call the showBlock to redraw the high-light block. The final code as below.
- <?xml version=”1.0″ encoding=”utf-8″?>
- <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” xmlns:components=”com.components.*”
- width=”420″ height=”160″ applicationComplete=”initApp();”>
- <mx:Script>
- <![CDATA[
- import mx.events.ScrollEvent;
- import mx.managers.ToolTipManager;
- private var updateTimer:Timer;
- private function initApp():void
- {
- ToolTipManager.showDelay = 0;
- //timer
- updateTimer = new Timer(5, 1);
- updateTimer.addEventListener(TimerEvent.TIMER, validate, false, 0, true);
- lightText.addEventListener(ScrollEvent.SCROLL, scrollEvent);
- lightText.htmlText = "Documentation for classes includes syntax, " +
- "<b><font size='18'>usage</font></b> information, and code samples for methods, " +
- "properties, " +
- "and event handlers and listeners for those APIs that belong to a specific class " +
- "in ActionScript (as opposed to global functions or properties). " +
- "The <font size='24'>classes</font> are listed alphabetically. " +
- "If you are not sure to which class a certain method or property belongs, " +
- "you can look it up in the Index.";
- beginIndexInput.text = "43";
- endIndexInput.text = "300";
- }
- private function showBlock():void
- {
- lightText.showBlock(parseInt(beginIndexInput.text), parseInt(endIndexInput.text));
- }
- //validate
- private function validate(event:TimerEvent):void
- {
- showBlock();
- }
- //scrollEvent
- private function scrollEvent(event:ScrollEvent):void
- {
- updateBlock();
- }
- //update
- private function updateBlock():void
- {
- updateTimer.reset();
- updateTimer.start();
- }
- ]]>
- </mx:Script>
- <components:BlockTextArea id=”lightText” width=”400″ height=”100″ y=”10″ x=”10″/>
- <mx:Button y=”118″ label=”Show Highlight” click=”showBlock();” right=”10″/>
- <mx:TextInput id=”beginIndexInput” x=”92″ y=”118″ width=”60″/>
- <mx:TextInput id=”endIndexInput” x=”232″ y=”118″ width=”60″/>
- <mx:Label x=”10″ y=”121″ text=”beginIndex:”/>
- <mx:Label x=”160″ y=”121″ text=”endIndex:”/>
- </mx:Application>
Then we start to test. Click the button “Debug”:
Click the button “Show Highlight”, you will find the blue high-light area:
When the rolling bar roll, it will call the way updateBlock. Redraw the high-light block by calling showBlock after 5ms.
( If beginIndex less than 0 or endInder bigger or equal to the text length. or endIndex less than beginIndex, it will not show the high-light block.)
If you feel the display method is too rigid, you could change the drawBlock in HighlightBlock or rewrite the drawBlock to create the effect such as wave line, underscores and etc.
If change the type of _textField from IUITextField to TextField and do relative change, the class HighlightBlock also could be applicated in ActionScript Project.
OK, now we have finished introducing the how to write and the use of method of HighlightBlock.
Cheers.

July 20th, 2008
Ntt.cc 








Posted in
Tags: 
RSS Feed
Email Feed
[...] 看看 [...]
Very nice tutorial
[...] Cheers. Permalink:http://ntt.cc/2008/07/20/how-to-highlight-the-text-in-flex-with-full-source-code.html [...]
[...] 注:本文英文版本:How to Highlight the Text in Flex(with Full Source Code) [...]
hi
sorry for the comment on the unappropriate post.
i want some sample code which generates the mxml file from database. i have to create different dyanamic forms to create the request.
can u please help me the same?
any help appriciated.
thanks
shripad
Thanks, really good stuff! I was really getting desperate about how to get TextField functions (getCharIndexAtPoint …) into a Flex app. Probably sounds trivial to you but I still haven’t fully understand all those conversions between TextFields, UIComponents and IUIComponents. Your example was really helpful to get this to work!
very good tutorial. the technique used was very similar to flexlib’s (http://code.google.com/p/flexlib/) highlighter class which was also using TextField. I have two questions: I’m wondering if its possible to accomplish the same effect on Text field and how hard is it and 2) Would your highlighter work on an Itemrenderer where you have a scrolling display / variable widths/heights? flexlib doesnt.
I meant Text class as opposed to TextField class
Would it be possible to apply this effect (block + custom tooltip) upon separate sentences of text into the same BlockTextArea ? This is what I’m actually looking for
In fact, I found out how to do it ! Thanks again for your explanations
Hi,
Nice tutorial..
sorry for the comment on the unappropriate post.
Can we do same highlight for
swf file created with pdf2swf -f optoin.
can u please help me the same?
any help appriciated.
Thanks
Atul
[...] How to Highlight the Text in Flex(with Full Source Code) – Ntt.cc [...]