如何跟踪 AS3 中动态文本字段中的文本部分

发布于 2024-12-28 03:33:50 字数 158 浏览 4 评论 0原文

我希望能够将非样式属性应用于 TextField 中的文本部分。例如,角色 30-45 将被设置为在某个方向上进行动画处理。

由于此字段是可编辑的,因此如果以任何方式编辑文本,则字符 30-45 可能不再是 30-45。

谁能想出一种优雅的方式来跟踪哪些角色应用了属性?

I want to be able to apply non-style attributes to sections of text in a TextField. For example characters 30-45 will be set to animate in a certain direction.

As this field is editable characters 30-45 may no longer be at 30-45 if the text is edited in any way.

Can anyone think of an elegant way to keep track of which characters had the attributes applied to them?

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

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

发布评论

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

评论(1

流星番茄 2025-01-04 03:33:50

我有一个类似的项目,最终扩展了 TextField 类来满足我的需求。这是要做什么的简短描述 - 恐怕我的实际代码是保密的:

  1. 覆盖 texthtmlText 的 setter
  2. 解析这些 setter 中的任何内容到自定义对象的数组中。每个对象都包含原始文本块和适用于它们的元数据(格式、注释等)。

    例如,

    Info; 
    

    将被转换为这样的对象:

    { text:"信息", clazz="sometext", 动画:true };
    
  3. 然后使用 appendText 逐块添加原始文本并使用 setTextFormat 应用来呈现实际的文本输出在每个附加步骤之后进行格式化(或执行其他必要的操作)。
  4. 添加事件侦听器以对 TEXT_INPUT 和/或 KEY_DOWN/KEY_UP 事件做出反应,以捕获任何新的用户输入。 (您将一遍又一遍地替换 TextField 的整个文本内容,因此不能使用 super.text。)
  5. 用户输入是通过使用 selectionBeginIndex 进行处理的, selectionEndIndex(计算对象数组的原始文本中的字符数,以找出哪些块受到影响)。直接在容器对象中添加或替换新文本,然后使用步骤 3. 刷新 TextField 中的整个文本。
  6. 我还添加了一种在渲染之前减少数组的方法(即将相邻块与相同的元数据组合起来)。这使数组保持精简,并有助于创建没有复杂树结构的 XML 输出(对于这种情况,一维正是我们所喜欢的)。
  7. 如果您在其他地方需要结果,请覆盖 texthtmlText 的 getter 以返回新格式化的信息。我使用 htmlText 返回一个完全修饰的 xml 字符串,并保留 text 用于访问原始文本内容,就像在通用 TextField 中一样。

I've had a similar project and ended up extending the TextField class to fit my needs. Here's a short description of what's to do - my actual code is confidential, I'm afraid:

  1. Override the setters for text and htmlText
  2. Parse any content from these setters into an array of custom objects. Each of these objects contains raw text chunks and the metadata that applies to them (format, comments, etc.).

    For example,

    <span class="sometext" animation="true">Info</span> 
    

    would be translated to an object like this:

    { text:"Info", clazz="sometext", animation:true };
    
  3. The actual text output is then rendered by using appendText to add chunk by chunk of the raw text and using setTextFormat to apply formatting (or do whatever else is necessary) after each append step.
  4. Add event listeners to react on TEXT_INPUT and/or KEY_DOWN/KEY_UP events to catch any new user input. (You will replace the entire text content of your TextField over and over again, so it's not an option to use super.text.)
  5. User input is processed by using selectionBeginIndex and selectionEndIndex (count the number of characters in the raw text of your object array to find out which chunks are affected). Add or replace the new text directly within the container objects, then use step 3. to refresh the entire text in the TextField.
  6. I have also added a method that reduces the array before it is rendered (i.e. combine adjacent chunks with identical metadata). This keeps the array lean and helps creating XML output that does not have a complicated tree structure (one-dimensional is quite what we like for this kind of scenario).
  7. Override the getters for text and htmlText to return the newly formatted info, if you need the results somewhere else. I've used htmlText to return a fully decorated xml string and kept text for accessing the raw text content, just like in a generic TextField.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文