使用 ActionScript 3.0 在 Flash 中突出显示单词
我正在使用Flash Professional CS4和actionscript 3.0制作一个文本编辑器,
它已经快完成了,我只需要添加一个功能来突出显示一些“标签”,例如“[NAME]”和“[AGE]”(通过更改其颜色)它们被写下来了。
我使用的是文本字段,而不是 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 );
}
}
此代码仅在第一次使用标签时有效,但如果再次使用标签,它不会改变颜色。
有什么想法吗?我还可以使用其他功能吗?
提前致谢。
I'm making a text editor using Flash professional CS4 and 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
taMain.text.indexOf(splitText[i])
将始终找到该单词的第一次出现,例如第一个“[NAME]”,并在第一次出现时设置文本格式,即使for 循环出现在“[NAME]”的另一处。indexOf() 采用第二个可选参数,用于索引的起始位置,因此您可以通过执行以下操作来跟踪当前文本中的位置:
但我不认为在空间上进行分割,就像 < code>var splitText:Array = taMain.text.split(" "),是在一般文本中查找单词的好方法。如果 [AGE] 是一行的最后一个单词,后面有一个换行符,或者 [NAME] 后面有一个逗号,例如“Hello [NAME], how are you”,该怎么办?上面的代码会错过这些事件。
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:
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.如果您的输入在 ASCII 范围内(例如没有德语变音符号),则使用正则表达式可以轻松找到单词/短语。然后,您可以将搜索词封装在 \b 中,如下所示:
这将匹配
MyVar
的每次出现,但前提是它是一个完整的单词。例如,MyVarToo
不会匹配,因为\b
指的是字边界。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:
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.