Skip to main content
March 31, 2011
Question

Highlight words

  • March 31, 2011
  • 2 replies
  • 902 views

I need to highlight some words (search results) in TLF text editor. I am using RichEditableText.

Please tell me how I can highlight more than one word at different places (in several paragraphs or spans).

I tried to use selectRange() function in RichEditableText. but it highlights only one occurence.

Thanks.

This topic has been closed for replies.

2 replies

Known Participant
April 1, 2011

Hi,

The same kind of stuff I have performed in my application . I am fully agreed with Robin's point but to be more explanatory you can do the following :

1. Find the begin and end of the text to be highligted. You can do it by using selection manager of text flow by the properties absoluteStart and absoluteEnd of selection manager

   like 

     var _selMgr:ISelectionManager = textFlow.interactionManager;

if(_selMgr.hasSelection())
            {
                if(_selMgr.focused)
                {
                    var begin:Number = _selMgr.absoluteStart;
                    var end:Number = _selMgr.absoluteEnd;

               }

        }

2. Once you get the begin and end you can get the start and end index of TextLine of the text flow like

   var startTextFlowLineIndex:int = textFlow.flowComposer.findLineIndexAtPosition(begin);
                    var endTextFlowLineIndex:int = textFlow.flowComposer.findLineIndexAtPosition(end);

and then get textline as

var textFlowLine:TextFlowLine;

textFlowLine = textFlow.flowComposer.getLineAt(startTextFlowLineIndex);

from text flow line you can get text line using

   textFlowLine.getTextLine()

methode

3. Get the bound of the first and last atom of the text lIne to draw a rectangle as

          var startbounds:Rectangle = textLine.getAtomBounds(textLine.getAtomIndexAtCharIndex(begin));
                var endbounds:Rectangle = textLine.getAtomBounds(textLine.getAtomIndexAtCharIndex(end));

Next very easy you can use sprite's graphic and drwa the rectangle.

Regards,

Gaurav Pandey

Adobe Employee
March 31, 2011

You can only *select* one contiguous region in TLF. Selection is more than just highlighting, since many operations apply to the selection, and the highlighting is really just a way to show the selection. But if you do your own highlighting, you can highlight whatever you'd like. There are a couple of different options that might get you what you need.

First, you can just apply a background color formatting property to the words you want highlighted. Not quite the same thing, but its very easy to program and will automatically update as you edit the text.

Second, if you want to build on top of TLF and manage the highlight yourself, you can use GeometryUtils.getHighlightBounds to map from a TextRange to the rectangle(s) occupied by the TextRange on screen. Then you can apply whatever visual effects you would like using Flash DisplayObjects. If the highlighting is done on a line by line basis, for example, you could use getHighlightBounds to create a Shape, make the Shape a child of the TextLine, and then (if you need to be robust across editing changes or resizes) just recreate the Shape each time the TextLine is recreated. There's a blog entry that describes how to tie into the line creation to attach decorations like this, see http://blogs.adobe.com/tlf/2011/01/decoration-example.html.

- robin