RichTextEditor コントロールを使用すると、ユーザーがテキストを入力、編集、およびフォーマットできます。ユーザーは、RichTextEditor コントロールの下部に表示されるサブコントロールを使用して、テキストフォーマットや URL リンクを適用します。
詳細については、『Adobe Flex リファレンスガイド』を参照してください。
RichTextEditor コントロールは、Panel コントロールと次の 2 つの直系の子で構成されています。
次の図は、フォーマットされたテキストを持つ RichTextEditor コントロールを示しています。
この例のソースについては、RichTextEditor コントロールの作成を参照してください。
RichTextEditor は、次に示すとおりインタラクティブに使用します。
RichTextEditor コントロールは、次の例に示すように、<mx:RichTextEditor> タグを使用して MXML 内で定義します。MXML の他の場所(他のタグまたは ActionScript ブロック)のコントロールを参照する場合は、id 値を指定します。
<?xml version="1.0"?> <!-- textcontrols/RichTextEditorControl.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:RichTextEditor id="myRTE" text="Congratulations, winner!" /> </mx:Application>前の例で実行する SWF ファイルは以下のとおりです。
フォーマットされていないテキストストリングを指定する場合は text プロパティを使用し、HTML 形式のストリングを指定する場合は htmlText プロパティを使用します。これらのプロパティの使用の詳細については、text プロパティの使用およびhtmlText プロパティの使用を参照してください。コントロール内でのテキストの選択、置換、フォーマットの詳細については、テキストの選択と修正を参照してください。
次の例では、RichTextEditor コントロールについての図を作成するためのコードを示しています。
<?xml version="1.0"?>
<!-- textcontrols/RichTextEditorControlWithFormattedText.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="700"
height="400">
<!-- The HTML text string used to populate the RichTextEditor control's
TextArea subcontrol. The text is on a single line. -->
<mx:Script><![CDATA[
[Bindable]
public var htmlData:String="<textformat leading='2'><p align='center'><b><font size='20'>HTML Formatted Text</font></b></p></textformat><br><textformat leading='2'><p align='left'><font face='_sans' size='12' color='#000000'>This paragraph contains<b>bold</b>, <i>italic</i>, <u>underlined</u>, and <b><i><u>bold italic underlined </u></i></b>text.</font></p></textformat><br><p><u><font face='arial' size='14' color='#ff0000'>This a red underlined 14-point arial font with no alignment set.</font></u></p><p align='right'><font face='verdana' size='12' color='#006666'><b>This a teal bold 12-pt.' Verdana font with alignment set to right.</b></font></p><br><li>This is bulleted text.</li><li><font face='arial' size='12' color='#0000ff'><u> <a href='http://www.adobe.com'>This is a bulleted link with underline and blue color set.</a></u></font></li>";
]]></mx:Script>
<!-- The RichTextEditor control. To reference a subcontrol prefix its ID with the RichTextEditor control ID. -->
<mx:RichTextEditor id="rte1"
backgroundColor="#ccffcc"
width="605"
headerColors="[#88bb88, #bbeebb]"
footerColors="[#bbeebb, #88bb88]"
title="Rich Text Editor"
htmlText="{htmlData}"
initialize="rte1.textArea.setStyle('backgroundColor', '0xeeffee')"
/>
</mx:Application>
前の例で実行する SWF ファイルは以下のとおりです。
このコントロールは、TextArea コントロールのテキストのサイズに応じてサイズ変更されることはありません。テキストが表示可能なスペースを超えた場合、デフォルトでは、TextArea コントロールによりスクロールバーが追加されます。height または width プロパティのいずれか(両方ではない)の値を指定すると、コントロールでは、設定していないプロパティに対してデフォルト値が使用されます。
RichTextEditor コントロールの幅が 605 ピクセル未満になるように width の値を設定すると、サブコントロールが複数行になります。
RichTextEditor サブコントロール(TextArea、ColorPicker、テキストフォーマットを制御する ComboBox や Button など)の設定はすべて、アプリケーションから制御できます。RichTextEditor サブコントロールを参照するには、要求するコントロール ID の前に RichTextEditor コントロールの ID を追加します。例えば、ID が rte1 である RichTextEditor コントロール内の ColorPicker コントロールを参照するには、rte1.colorPicker を使用します。
RichTextEditor コントロールに直接適用する継承可能なスタイルは、基になる Panel コントロールとそのサブコントロールに影響します。RichTextEditor コントロールに直接適用するプロパティは、基になる Panel コントロールのみに影響します。
詳細については、『Adobe Flex リファレンスガイド』の RichTextEditor を参照してください。
RichTextEditor サブコントロールのプロパティとスタイルの設定
次に示す簡単な例は、RichTextEditor コントロールおよびそのサブコントロールのプロパティとスタイルを設定および変更する方法を示しています。この例では、RichTextEditor コントロールが Panel クラスから継承したスタイルを使用して、Panel コントロールのヘッダと ToolBar コンテナのカラーを設定します。また、RichTextEditor コントロールの creationComplete イベントメンバー内の TextArea コントロールの背景色を設定します。ユーザーがボタンをクリックすると、このクリックのイベントリスナーにより、TextArea コントロールの背景色と ColorPicker コントロールの選択済みカラーが変更されます。
<?xml version="1.0"?>
<!-- textcontrols/RTESubcontrol.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
height="420">
<!-- The RichTextEditor control. To set the a subcontrol's style or property, fully qualify the control ID. The footerColors style sets the ControlBar colors. -->
<mx:RichTextEditor id="rte1"
backgroundColor="#ccffcc"
headerColors="[#88bb88, #bbeebb]"
footerColors="[#bbeebb, #88bb88]"
title="Rich Text Editor"
creationComplete="rte1.textArea.setStyle('backgroundColor','0xeeffee')"
text="Simple sample text"
/>
<!-- Button to set a white TextArea background. -->
<mx:Button
label="Change appearance"
click="rte1.textArea.setStyle('backgroundColor', '0xffffff');rte1.colorPicker.selectedIndex=27;"
/>
<!-- Button to reset the display to its original appearance. -->
<mx:Button
label="Reset Appearance"
click="rte1.textArea.setStyle('backgroundColor', '0xeeffee');rte1.colorPicker.selectedIndex=0;"
/>
</mx:Application>
前の例で実行する SWF ファイルは以下のとおりです。
整列ボタンのような標準的な RichTextEditor サブコントロールは、いずれも削除できます。また、検索および置換のダイアログボックスをポップアップ表示させるボタンのような、独自のサブコントロールを追加することもできます。
次の例では、RichTextEditor コントロールから整列ボタンを削除し、2 番目の RichTextEditor コントロールのデフォルトの外観を表示します。
<?xml version="1.0"?>
<!-- textcontrols/RTERemoveAlignButtons.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
public function removeAlignButtons():void {
rt1.toolbar.removeChild(rt1.alignButtons);
}
]]></mx:Script>
<mx:RichTextEditor id="rt1" title="RichTextEditor With No Align Buttons" creationComplete="removeAlignButtons()"/>
<mx:RichTextEditor id="rt2" title="Default RichTextEditor"/>
</mx:Application>
前の例で実行する SWF ファイルは以下のとおりです。
<mx:RichTextEditor id="rt" initialize="addMyControl()"
次の例では、RichTextEditor コントロールに検索および置換のダイアログボックスを追加します。これはアプリケーションと、検索および置換のダイアログを定義する(さらにテキスト上で検索および置換の操作も実行する)カスタムの TitleWindow コントロールという、2 つのファイルで構成されます。アプリケーションには、次のような TitleWindow をポップアップ表示させるボタンを追加する関数が含まれています。
<?xml version="1.0"?>
<!-- textcontrols/CustomRTE.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="600" height="100%">
<mx:Script>
<![CDATA[
import mx.controls.*;
import mx.containers.*;
import flash.events.*;
import mx.managers.PopUpManager;
import mx.core.IFlexDisplayObject;
// The variable for the pop-up dialog box.
public var w:IFlexDisplayObject;
// Add the Find/Replace button to the Rich Text Editor control's
// tool bar container.
public function addFindReplaceButton():void {
var but:Button = new Button();
but.label = "Find/Replace";
but.addEventListener("click",findReplaceDialog);
rt.toolbar.addChild(but);
}
// The event listener for the Find/Replace button's click event
// creates a pop-up with a MyTitleWindow custom control.
public function findReplaceDialog(event:Event):void {
var w:MyTitleWindow = MyTitleWindow(PopUpManager.createPopUp
(this, MyTitleWindow, true));
w.height=200;
w.width=340;
// Pass the a reference to the textArea subcontrol
// so that the custom control can replace the text.
w.RTETextArea = rt.textArea;
PopUpManager.centerPopUp(w);
}
]]>
</mx:Script>
<mx:RichTextEditor id="rt" width="95%" title="RichTextEditor"
text="This is a short text."
initialize="addFindReplaceButton()"/>
</mx:Application>
前の例で実行する SWF ファイルは以下のとおりです。
次の MyTitleWindow.mxml ファイルは、検索および置換のインターフェイスとロジックを含むカスタムの myTitleWindow コントロールを定義します。
<?xml version="1.0"?>
<!-- A TitleWindow that displays the X close button. Clicking the close button only generates a CloseEvent event, so it must handle the event to close the control. -->
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" title="Find/Replace" showCloseButton="true" close="closeDialog();">
<mx:Script>
<![CDATA[
import mx.controls.TextArea;
import mx.managers.PopUpManager;
// Reference to the RichTextArea textArea subcontrol.
// It is set by the application findReplaceDialog method
// and used in the replaceAndClose method, below.
public var RTETextArea:TextArea;
// The event handler for the Replace button's click event.
// Replace the text in the RichTextEditor TextArea and
// close the dialog box.
public function replaceAndClose():void{
RTETextArea.text = RTETextArea.text.replace(ti1.text, ti2.text);
PopUpManager.removePopUp(this);
}
// The event handler for the TitleWindow close button.
public function closeDialog():void {
PopUpManager.removePopUp(this);
}
]]>
</mx:Script>
<!-- The TitleWindow subcontrols: the find and replace inputs,
their labels, and a button to initiate the operation. -->
<mx:Label text="Find what:"/>
<mx:TextInput id="ti1"/>
<mx:Label text="Replace with:"/>
<mx:TextInput id="ti2"/>
<mx:Button label="Replace" click="replaceAndClose();"/>
</mx:TitleWindow>
このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート