Flex:修改文本区域

时间:2009-03-11 04:48:36

标签: flex textarea

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

    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>

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

2 个答案:

答案 0 :(得分: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();
        }

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

答案 1 :(得分:0)

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

中找到它

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