开发者

Word highlighting in Flash using ActionScript 3.0

开发者 https://www.devze.com 2023-02-23 01:31 出处:网络
I\'m making a text editor using Flash professional CS4 and actionscript 3.0 It\'s nearly finished, I only need to add a fun开发者_运维技巧ction that highlights some \"tags\" like \"[NAME]\" and \"[AG

I'm making a text editor using Flash professional CS4 and actionscript 3.0

It's nearly finished, I only need to add a fun开发者_运维技巧ction that highlights some "tags" like "[NAME]" and "[AGE]" (by changing its color) whenever they are written.

I'm using a textField, not a TextArea component.This is the code i'm using, but it doesn't work as planned.

taMain.addEventListener(Event.CHANGE, checkTags);
function checkTags(e):void{
    var tempFormat:TextFormat = taMain.getTextFormat(taMain.selectionBeginIndex - 1, taMain.selectionEndIndex);
    var splitText:Array = taMain.text.split(" ");
    for (var i = 0; i < splitText.lenght; i++) {
        switch (splitText[i]) {
            case "[NAME]":
                tempFormat.color = (0xff0000);
            break;
            case "[AGE]":
                tempFormat.color = (0x0000ff);
            break;
            default:
                tempFormat.color = (0x000000);
        }
        taMain.setTextFormat(tempFormat, taMain.text.indexOf(splitText[i]), taMain.text.indexOf(splitText[i]) + splitText[i].length );
    }
}

This code works only the first time the tag is used, but it doesn't change the color if tag is used again.

Any ideas? any other function i could use?

Thanks in advance.


taMain.text.indexOf(splitText[i]) will always find the first occurrence of the word, like the first "[NAME]", and set the text format on that first occurrence, even if the for-loop is at another occurrence of "[NAME]".

indexOf() takes a second optional parameter, for an index to start from, so you could keep track of where in the text you currently are, by doing something like this:

var tempFormat:TextFormat = taMain.getTextFormat(taMain.selectionBeginIndex - 1, taMain.selectionEndIndex);
var splitText:Array = taMain.text.split(" ");
var startIndex:Number = 0;
for (var i = 0; i < splitText.length; i++) {
    switch (splitText[i]) {
        case "[NAME]":
            tempFormat.color = (0xff0000);
        break;
        case "[AGE]":
            tempFormat.color = (0x0000ff);
        break;
        default:
            tempFormat.color = (0x000000);
    }
    taMain.setTextFormat(tempFormat, taMain.text.indexOf(splitText[i], startIndex), taMain.text.indexOf(splitText[i], startIndex) + splitText[i].length );
    startIndex = taMain.text.indexOf(splitText[i], startIndex) + splitText[i].length;
}

But I don't think that splitting on space, like in var splitText:Array = taMain.text.split(" "), is a good way to find words in general text. What if [AGE] is the last word of a line, with a line break after it, or there's a comma after [NAME], like in "Hello [NAME], how are you"? The code above would miss those occurrences.


With regular expressions it's easy to find a word/phrase if your input is in the ASCII range (no German umlauts e.g.). Then you can encapsulate your search term in \b's like so:

/\bMyVar\b/g 

This will match every occurence of MyVar but only if it's a whole word. MyVarToo, e.g., wouldn't be matched because \b refers to a word boundary.

0

精彩评论

暂无评论...
验证码 换一张
取 消