【问题标题】:Flex Spark Replace text to color textFlex Spark 将文本替换为彩色文本
【发布时间】:2012-06-06 17:50:08
【问题描述】:

我正在制作简单的编辑器。我想要这样,当我写任何文本时,让我们将 RichEditableText 中的无色文本替换为彩色文本。

例如: PHP --> <s:span color="#FF0000">PHP<s:span>

此代码无效:

editorum.text = editorum.text.replace('PHP','<s:span color="#FF0000">php<s:span>');

编辑为:&lt;s:RichEditableText id="editorum" &gt;&lt;/s:RichEditableText&gt;

Click here and try

源代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               width="319" height="198" minWidth="955" minHeight="600">
    <fx:Style source="ops.css"/>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import flashx.textLayout.formats.TextLayoutFormat;
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            import spark.events.TextOperationEvent;
            import spark.utils.TextFlowUtil;
            protected function editorum_changeHandler(event:TextOperationEvent):void
            {
                editorum.text = editorum.text.replace('melon','<s:span color="#FF0000">melon<s:span>');
            }
        ]]>
    </fx:Script>
    <s:RichEditableText id="editorum" x="10" y="28" width="299" height="159"
                        change="editorum_changeHandler(event)">

    </s:RichEditableText>
    <s:Label x="10" y="10" width="119" text="Write 'melon' :"/>
</s:Application>

另一个问题;最后如何发送指针?当我按任意键时,指针将要处理。试试看:http://samed.us/samples/ops3.swf

感谢您的帮助...

【问题讨论】:

  • 我不清楚你想做什么。这是 PHP 问题还是 Flex 问题?尤其是这一行:“我想要这样,当我将编写 PHP 时,让我们将 RichEditableText 中的无色文本替换为彩色文本。”尤其令人困惑。另外,您提到了一个不起作用的代码段;请扩大“不起作用”。你有编译时错误吗?运行时错误?还是别的什么?
  • 对不起我的英语。这样不好。这是弹性问题。 PHP 是示例文本。没关系PHP。你认为任何文本。例如“瓜”。当我在 RichEditableText 上写瓜时,函数将颜色文本更改为“瓜”。
  • 好的,根据您的示例行为,看起来它正确地将键入的文本“melon”替换为可以赋予其颜色的跨度。这使我想到了原始问题的第二部分;究竟是什么问题?
  • 分享您的样本来源。

标签: apache-flex mobile replace flex-spark


【解决方案1】:

代替这一行:

editorum.text = editorum.text.replace('melon','<s:span color="#FF0000">melon<s:span>');

试试这样的:

var text : String = editorum.text.replace('melon','<s:span color="#FF0000">melon<s:span>')
editorum.textFlow = TextConverter.importToFlow(text, TextConverter.TEXT_FIELD_HTML_FORMAT);

基本上,您需要将 HTML 转换为 TLF 框架可以理解的内容。 Read some docs 在 textFlow 和 TextConverter 上

【讨论】:

  • 好的,这段代码可以工作了。另一个问题;最后如何发送指针?当我按任意键时,指针将要处理。试试看:samed.us/samples/ops3.swf
  • 如果这篇文章回答了您的问题,请务必将其标记为此类。如果您有新问题,请将其作为新问题发布。我假设您需要在更改 textFlow 后重置光标位置。
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多