ActionScript:如何对 TextArea 内容应用不同的格式

发布于 2024-11-08 00:00:46 字数 885 浏览 0 评论 0原文

我有一个名为 labels 的 TextArea 组件,它将由名为 labelsArray 的数组中的字符串填充。

labelsArray 内容的示例:

private var labelsArray:Array = new Array( "apple", "*banana", "carrot" );

最初,我使用以下语句填充标签:

labels.text = labelsArray.join( "\n" );

如果字符串前面有星号 (*) - 就像示例内容中的“banana”一样 - 该字符串需要采用不同的颜色并且是用方括号 [] 括起来。星号本身不显示。

我找不到任何应用于字符串的格式。

我发现 TextFormat 类可通过 setTextFormat 应用于 TextField 对象。

我还发现 TextLayoutFormat 类可通过 setFormatOfRange 应用于 TextArea 对象。不幸的是,我不明白对 setFormatOfRange 参数的解释:

setFormatOfRange( format,anchorPosition, activePosition ) anchorPosition - 相对于文本字符串开头的字符位置,指定当使用箭头键扩展选区时保持固定的选区末尾。 activePosition - 相对于文本字符串开头的字符位置,指定使用箭头键扩展选区时移动的选区末尾。

我不明白从“...指定结束...”开始到结束。我不明白箭头键如何与文本格式一起发挥作用。

还有比这更简单的事情吗?我希望循环遍历 labelsArray 并根据需要在每个字符串上应用格式,然后将其连接/附加到 labels.text。

感谢您的帮助! 邦妮

I have a TextArea component called labels that will be populated by Strings in an Array called labelsArray.

Example of labelsArray content:

private var labelsArray:Array = new Array( "apple", "*banana", "carrot" );

Initially, I populated labels with this statement:

labels.text = labelsArray.join( "\n" );

If the String is preceded by an asterisk(*) - as is the case with "banana" in the sample content - that String needs to be in a different color AND be surrounded by square brackets []. The asterisk itself is not displayed.

I was not able to find any formatting applied to Strings.

I found out about the TextFormat class to be applied to TextField objects with setTextFormat.

I also found out about the TextLayoutFormat class to be applied to TextArea objects with setFormatOfRange. Unfortunately, I don't under the explanations given for the parameters of setFormatOfRange:

setFormatOfRange( format, anchorPosition, activePosition )
anchorPosition - A character position, relative to the beginning of the text String, specifying the end of the selection that stays fixed when the selection is extended with the arrow keys.
activePosition - A character position, relative to the beginning of the text String, specifying the end of the selection that moves when the selection is extended with the arrow keys.

I don't understand starting from "...specifying the end..." to the end. I don't understand how arrow keys come into play with formatting of text.

Is there something much simpler than this? I'm hoping to loop through labelsArray and apply formatting if needed on each String, and then concatenating/appending it to labels.text.

Thanks for your help!
Bonnie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-11-15 00:00:46

一种方法是使用 htmlText 属性。

myTextArea.htmlText = "<font color="red" size="12">String1</font>,
  <font color="green" size="11">String2</font>";

One way is to use htmlText property.

myTextArea.htmlText = "<font color="red" size="12">String1</font>,
  <font color="green" size="11">String2</font>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文