【问题标题】:Flex: Modifying Text AreaFlex:修改文本区域
【发布时间】:2010-10-12 14:43:15
【问题描述】:

我正在使用文本区域制作文本编辑器。哪个用户可以更改字体大小、系列等。
这是我的代码:

    private function ChangeFont(event: Event):void
       {
        var mySelectedTextRange:TextRange = new TextRange(thistxtarea,true,
                                                thistxtarea.selectionBeginIndex,
                                                thistxtarea.selectionEndIndex);
        mySelectedTextRange.fontSize = int(cmbbxFntSze.text);
        thistxtarea.setFocus();
       }

我有这个组合框来输入所需的字体大小:

<mx:ComboBox x="78" y="8" width="114" id="cmbbxFntFam"  close="ChangeFont(event)"></mx:ComboBox>

如果里面的文字没有突出显示,我该如何更改字体属性?例如,我将鼠标指针放在文本区域内的最后一个文本索引上,然后在组合框中选择所需的字体大小。在文本区域中输入的字母的以下字体大小应为组合框上选择的字体大小。我发布的代码只有在我突出显示所需的文本时才有效。

【问题讨论】:

    标签: apache-flex textarea


    【解决方案1】:

    这是设置样式

    private function setTextStyles(type:String, value:Object = null):void
            {
                if(thisindex != -1)
                {
                    var tf:TextFormat;
    
                    var beginIndex:int = textArea.getTextField().selectionBeginIndex;
                    var endIndex:int = textArea.getTextField().selectionEndIndex;
    
                    textArea.getTextField().alwaysShowSelection = true;
    
                    if (beginIndex == endIndex)
                    {
                        tf = previousTextFormat;
                    }
                    else    
                        tf = new TextFormat();
    
    
                    if (type == "bold" || type == "italic" || type == "underline")
                    {
                        tf[type] = value;
                    }
                    else if (type == "align")
                    {
                        if (beginIndex == endIndex)
                        {
                            tf = new TextFormat();
                        }
    
                        // Apply the paragraph styles to the whole paragraph instead of just 
                        // the selected text
                        beginIndex = textArea.getTextField().getFirstCharInParagraph(beginIndex) - 1;
                        beginIndex = Math.max(0, beginIndex);
                        endIndex = textArea.getTextField().getFirstCharInParagraph(endIndex) +
                            textArea.getTextField().getParagraphLength(endIndex) - 1;
                        tf[type] = value;
                        previousTextFormat[type] = value;
                        if (!endIndex)
                            textArea.getTextField().defaultTextFormat = tf;
                    }
                    else if (type == "font")
                    {
                        tf[type] = cmbbxFntFam.text;
                    }
                    else if (type == "size")
                    {
                        var fontSize:uint = uint(cmbbxFntSze.text);
                        if (fontSize > 0)
                            tf[type] = fontSize;
                    }
                    else if (type == "color")
                    {
                        tf[type] = uint(clrpckerFontColor.selectedColor);
                    }
    
    
                    textFormatChanged = true;
    
                    if (beginIndex == endIndex)
                    {                       
                        previousTextFormat = tf;
                    }
                    else
                    {
                        textArea.getTextField().setTextFormat(tf,beginIndex,endIndex);//textArea.setTextFormat(tf,beginIndex,endIndex);
                    }
    
                    dispatchEvent(new Event("change"));
    
                    var caretIndex:int = textArea.getTextField().caretIndex;
                    var lineIndex:int = textArea.getTextField().getLineIndexOfChar(caretIndex);
    
                    textArea.invalidateDisplayList();
                    textArea.validateDisplayList();
                    textArea.validateNow();
    
                    // Scroll to make the line containing the caret under viewable area
                    while (lineIndex >= textArea.getTextField().bottomScrollV)
                    {
                        textArea.verticalScrollPosition++;
                    }
    
                    callLater(textArea.setFocus);
    
                }
            }
    

    此代码用于从 textArea 获取样式

     private function getTextStyles():void
            {               
    
                if (!textArea)
                    return;
    
                var tf:TextFormat;
    
                var beginIndex:int = textArea.getTextField().selectionBeginIndex;
                var endIndex:int = textArea.getTextField().selectionEndIndex;
    
                if (textFormatChanged)
                    previousTextFormat = null;
    
                if (beginIndex == endIndex)
                {
                    tf = textArea.getTextField().defaultTextFormat;
                    if (tf.url != "")
                    {
                        var carIndex:int = textArea.getTextField().caretIndex;
                        if (carIndex < textArea.getTextField().length)
                        {
                            var tfNext:TextFormat=textArea.getTextField().getTextFormat(carIndex, carIndex + 1);
    
                            if (!tfNext.url || tfNext.url == "")
                                tf.url = tf.target = "";
                        }
                        else
                            tf.url = tf.target = ""; 
                    }
                }
                else
                    tf = textArea.getTextField().getTextFormat(beginIndex,endIndex);                
    
    
                if (cmbbxFntSze.text != tf.font)
                    setComboSelection(cmbbxFntFam, tf.font);
                if (int(cmbbxFntSze.text) != tf.size)
                    setComboSelection(cmbbxFntSze,String(tf.size));
                if (clrpckerFontColor.selectedColor != tf.color)
                    clrpckerFontColor.selectedColor = Number(tf.color);
    
                if (btnBold.selected != tf.bold)
                    btnBold.selected = tf.bold;//Alert.show("bold");
                if (btnItalic.selected != tf.italic)
                    btnItalic.selected = tf.italic;
                if (btnUnderline.selected != tf.underline)
                    btnUnderline.selected = tf.underline;
    
    
                if (tf.align == "left")
                    alignButtons.selectedIndex = 0;
                else if (tf.align == "center")
                    alignButtons.selectedIndex = 1;
                else if (tf.align == "right")
                    alignButtons.selectedIndex = 2;
                else if (tf.align == "justify")
                    alignButtons.selectedIndex = 3;
    
    
    
                if (textArea.getTextField().defaultTextFormat != tf)
                    textArea.getTextField().defaultTextFormat = tf;
                previousTextFormat = tf;
                textFormatChanged = false;
    
                lastCaretIndex = textArea.getTextField().caretIndex;                
                thishtmltxt = textArea.htmlText;
                textArea.validateNow();
            }
    

    请检查小错误,因为当我编码时,我有一些注释痕迹

    【讨论】:

      【解决方案2】:

      您是否看过 mx.controls.RichTextEditor 是如何做到这一点的? 您可以在 ...\frameworks\projects\framework\src\mx\controls

      中找到它

      如果您扫描该代码,您将看到 RichTextEditor 在其维护的 TextFormat 变量中保留当前文本样式设置,然后将该样式应用于新输入的文本。当用户更改字体/大小或选择更改以获取相邻样式时,该变量会更新。对于 selectionBeginIndex == selectionEndIndex 的情况也有特殊考虑。

      【讨论】:

      • 你好。我查看了您的建议,但是有一些我不理解的代码,例如 textArea.getTextField().setTextFormat textArea has no properties getTextField.它来自哪里?请帮我解决一下。谢谢
      • getTextField() 位于 mx_internal 命名空间中。您需要将该命名空间导入到您的类中,然后打开它,或者显式添加 getTextField() 前缀(即 mx_internal::getTextField())
      • 你好。任何人都在运行时创建 TextArea 时实现了这一点?请给我举个例子来说明如何做到这一点?我很难在覆盖函数中进行跟踪。谢谢你
      猜你喜欢
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2010-11-04
      • 2013-05-18
      • 1970-01-01
      相关资源
      最近更新 更多