Skip to main content
Participant
April 19, 2010
Question

Find/Search and modify in TLF

  • April 19, 2010
  • 1 reply
  • 649 views

Can anybody point me in a good direction for searching the text inside of a TextFlow and finding text and then modifing it? I can highlight words and change them just fine by using the selectionState - but, I'm just stumped how to search and then modify. By modify, I'm just changing the TextLayoutFormat of that word.

Any help would be awesome.

Thank you,

Mike

This topic has been closed for replies.

1 reply

Adobe Employee
April 19, 2010

You can use the selection for modifying as well. Assuming the textflow's interactionManager is an IEditManager, you can call applyFormat. It can apply TextLayoutFormats to the leaf node (span level), paragraph, or container that is associated with the current selection.

- robin

Participant
April 19, 2010

Thanks Robin.

I was looking for the way to determine were the word was:

private function checkForMatchingWord():void 
            {
                var currentLeaf:FlowLeafElement = _textFlow.getFirstLeaf();
                var currentParagraph:ParagraphElement = currentLeaf ? currentLeaf.getParagraph() : null;
               
                while (currentParagraph) { // iterate over all paragraphs in the text flow
                    var paraStart:uint = currentParagraph.getAbsoluteStart();
                    var currWordBoundary:int = 0;
                    var nextWordBoundary:int = 0;
                    var pattern:RegExp = new RegExp(wordToHighlight, "i");
                   
                    while (true) { // iterate over letters in a word
                        nextWordBoundary = currentParagraph.findNextWordBoundary(currWordBoundary);
                        if (nextWordBoundary == currWordBoundary) {
                            break; // end of paragraph; break of inner while loop
                        }
                        var word:String = '';
                        var indexInLeaf:int = currWordBoundary + paraStart - currentLeaf.getAbsoluteStart();
                        var wordLen:uint = nextWordBoundary - currWordBoundary;
                       
                        while (true){ // traverse consecutive leaf elements looking for 'wordLen' characters
                                                           
                            // Take as many characters from the current leaf as possible to meet the quota of 'wordLen'
                            var consumeCount:uint = indexInLeaf + wordLen <= currentLeaf.textLength ? wordLen : currentLeaf.textLength - indexInLeaf;
                            word += currentLeaf.text.substr (indexInLeaf, consumeCount);
                                                                           
                            wordLen -= consumeCount;
                            if (!wordLen)
                                break;
                               
                            // Quota not met; move to the next leaf
                            // Also reset the index where the next leaf will be scanned
                            currentLeaf = currentLeaf.getNextLeaf();
                            indexInLeaf = 0;
                        }
                       
                        if (pattern.test(word))
                        {                                   
                            highlightWord(currWordBoundary, nextWordBoundary, paraStart);
                        }
                       
                        currWordBoundary = nextWordBoundary;
                    }
                                           
                    // At this point, currentLeaf is the last leaf in currentParagraph. Move to the next paragraph.
                    currentLeaf = currentLeaf.getNextLeaf();
                    currentParagraph = currentLeaf ? currentLeaf.getParagraph() : null;                   
                }
                //Set the focus back to the TextFlow object
                _textFlow.interactionManager.setFocus();
               
            }

private function highlightWord(begin:int, end:int, paraStart:int):void
            {
                var absoluteBegin:int = paraStart + begin;
                var absoluteEnd:int = paraStart + end -1; //minus so you don't get the space after
               
                //Check for null
                if(_textFlow && _textFlow.interactionManager is IEditManager)
                {
                    //Get what is selected
                    var selectionState:SelectionState = new SelectionState(textFlow, absoluteBegin, absoluteEnd);
                   
                    //Start with the default textFormat and apply the 'common' which is from what is selected
                    var cf:TextLayoutFormat = defaultTextFormat;
                    cf.apply(_textFlow.interactionManager.getCommonCharacterFormat());
                    cf.backgroundColor = highLightColor;
                   
                    //Apply the formatting change
                    IEditManager(_textFlow.interactionManager).applyLeafFormat(cf, selectionState);
                   
                }
            }

Thanks for you response Robin.