2009-03-11 19 views
2

私はテキストエリアを使ってテキストエディタを作っています。フォントサイズ、家族やなどを変更することができ、どのユーザー
は、これは、上の私のコードです:フレックス:テキストエリアを変更する

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

私はフォントを変更するにはどうすればよい:

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

私は希望するフォントサイズを入力するには、このコンボボックスを持っています内部のテキストが強調表示されていない場合のプロパティたとえば、私は、テキスト領域内のテキストの最後のインデックスにマウスポインタを合わせ、コンボボックスで目的のフォントサイズを選択します。テキスト領域に入力された文字の次のフォントサイズは、コンボボックスで選択したフォントサイズにする必要があります。投稿したコードは、目的のテキストを強調表示する場合にのみ機能します。

答えて

1

これは私がいくつかコメントしているこれをコーディングする際に、軽微なエラーのだってを確認してください。このコードはに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(); 
     } 

からスタイルを取得するために使用されたスタイルに

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); 

      } 
     } 

を設定することですトレース

0

mx.controls.RichTextEditorこれはどうでしょうか? あなたがそれを見つけることができます... \枠組み\プロジェクト\枠組み\ SRC \ MX \コントロール

あなたはのRichTextEditorはそれが維持されるTextFormat変数に現在のテキストスタイル設定を保持していることがわかりますそのコードをスキャンした場合

、新しく入力したテキストにそのスタイルを適用します。この変数は、ユーザーがフォント/サイズを変更したとき、または選択が変更されて隣接スタイルを取得したときに更新されます。 selectionBeginIndex == selectionEndIndexの場合も特に考慮されます。

+0

こんにちは。私はあなたが提案したものを見直しますが、このtextArea.getTextField()。setTextFormat textAreaのような理解できないコードがありますが、getTextField.Whereというのはどこから来たのですか?ありがとうございます。ありがとうございました – Jejad

+0

getTextField()はmx_internal名前空間にあります。その名前空間をクラスにインポートして開き、明示的にgetTextField()(つまりmx_internal :: getTextField())の前に接頭辞 –

+0

を追加する必要があります。誰でもこれをTextAreaのランタイム作成時に実装しましたか?私に例を教えてください。オーバーライドでトレースするのは苦労します。 – Jejad

関連する問題