简体   繁体   English

使用ActionScript 3.0在Flash中突出显示单词

[英]Word highlighting in Flash using ActionScript 3.0

I'm making a text editor using Flash professional CS4 and actionscript 3.0 我正在使用Flash Professional CS4和ActionScript 3.0创建文本编辑器

It's nearly finished, I only need to add a function that highlights some "tags" like "[NAME]" and "[AGE]" (by changing its color) whenever they are written. 它快要完成了,我只需要添加一个函数就可以在每次编写时突出显示一些“标记”,例如“ [NAME]”和“ [AGE]”(通过更改其颜色)。

I'm using a textField, not a TextArea component.This is the code i'm using, but it doesn't work as planned. 我使用的是textField而不是TextArea组件。这是我正在使用的代码,但无法按计划工作。

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. 提前致谢。

With regular expressions it's easy to find a word/phrase if your input is in the ASCII range (no German umlauts eg). 使用正则表达式,如果您的输入在ASCII范围内(例如,没有德文变音符号),则很容易找到单词/短语。 Then you can encapsulate your search term in \\b's like so: 然后,您可以将搜索字词封装在\\ b中,如下所示:

/\bMyVar\b/g 

This will match every occurence of MyVar but only if it's a whole word. 这将匹配MyVar每次出现,但MyVar是它是一个完整的单词。 MyVarToo , eg, wouldn't be matched because \\b refers to a word boundary. 例如, MyVarToo不会被匹配,因为\\b表示单词边界。

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]". taMain.text.indexOf(splitText[i])始终会找到单词的第一个出现,就像第一个“ [NAME]”一样,并在第一个出现上设置文本格式,即使for循环在另一个出现的“ [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: indexOf()带有第二个可选参数,用于从索引开始,因此您可以通过执行以下操作来跟踪当前文本中的位置:

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. 但是我不认为像在var splitText:Array = taMain.text.split(" ")那样在空间上拆分不是在普通文本中查找单词的好方法。 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"? 如果[AGE]是一行的最后一个单词,后跟一个换行符,或者[NAME]之后有一个逗号,例如“ Hello [NAME],你好吗”? The code above would miss those occurrences. 上面的代码将丢失这些情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM