lgmathii
lgmathii

Reputation: 1

How to highlight static text background in AS3 dynamically?

If i can search a word in search box that word will find static text then highlight appear in flash as3. Any suggest please?

Upvotes: 0

Views: 682

Answers (1)

Marty
Marty

Reputation: 39456

You can use .getCharBoundaries(), which returns a Rectangle encompassing a character at a given index in your TextField.

Using that Rectangle, you can create some highlight graphics. Here's a function that will simplify that process:

function highlightChar(textField:TextField, charIndex:int):void
{
    var rect:Rectangle = textField.getCharBoundaries(charIndex);
    var box:Shape = new Shape();

    box.blendMode = BlendMode.MULTIPLY;
    box.graphics.beginFill(0xFFCC33);
    box.graphics.drawRect(textField.x + rect.x, textField.y + rect.y, rect.width, rect.height);
    box.graphics.endFill();

    if(textField.parent) textField.parent.addChild(box);
}

From here, you can create another function that will accept a phrase to highlight:

function highlightPhrase(textField:TextField, phrase:String):void
{
    var start:int = textField.text.indexOf(phrase);

    if(start >= 0)
    {
        for(var i:int = start; i < phrase.length; i++)
        {
            highlightChar(textField, i);
        }
    }
}

Combined, you'll find it easily to highlight a block of text like this:

var t:TextField = new TextField();
t.text = "This text is highlighted";
addChild(t);

highlightPhrase(t, "This text");

Upvotes: 1

Related Questions