Use textrange to select and modify strings in richtexteditor textarea

Source: Internet
Author: User

Http://livedocs.adobe.com/flex/3/html/help.html? Content1_textcontrols_05.html

 

Selecting and modifying text

You can select and modify text in textarea, textinput, and richtexteditor controls. To change a label or text control's text, assign a new value to the control'sTextOrHtmltextProperty. For more information onHtmltextProperty, see using the htmltext property.

Selecting text

The flex editable controls provide properties and methods to select text regions and get selections. You can modify the contents of the selection as described in modifying text.

Creating a selection

The textinput and textarea controls, including the richtexteditor control's textarea subcontrol, provide the following text selection properties and method:

    • Setselection ()Method selects a range of text. You specify the zero-based indexes of the Start character and the position immediatelyAfterThe last character in the text.
    • SelectionbeginindexAndSelectionendindexSet or return the zero-based location in the text of the start and position immediatelyAfterThe end of a selection.

To select the first 10 characters of the mytextarea textarea control, for example, use the following method:

 
Mytextarea. setselection (0, 10 );

To change the last character of this selection to be the twenty-character th character in the textarea control, use the following statement:

Mytextarea. endindex = 25;

To select text in a richtexteditor control, use the control's textarea subcontrol, which you access by usingTextareaId. To select the first 10 characters in the myrte richtexteditor control, for example, use the following code:

 
Myrte. textarea. setselection (0, 10 );

Getting a selection

You get a text control's selection by getting a textrange object with the selected text. you can then use the textrange object to modify the selected text, as described in modifying text. the technique you use to get the selection depends on the control type.

Get the selection in a textarea or textinput Control

Use the textrange class constructor to get a textrange object with the currently selected text in a textarea or textinput control. For example, to get the current selection of the mytextarea control, use the following line:

 
VaR myselectedtextrange: textrange = new textrange (mytextarea, true );

The second parameter,True, Tells the constructor to return a textrange object with the selected text.

Get the selection in a richtexteditor Control

UseSelectionRead-Only property of the richtexteditor To Get A textrange object with the currently selected text in its textarea subcontrol. you can use the textrange object to modify the selected text, as described in modifying text. for example, to get the current selection of the myrte richtexteditor control, us the following line:

Public var myselectedtextrange: textrange = myrte. selection;
Modifying text

You use the textrange class to modify the text in a textarea, textinput, or richtexteditor control. This class lets you affect the following text characteristics:

    • TextOrHtmltextProperty Contents
    • Text color, decoration (underlining), and alignment
    • Font family, size, style (italics), and weight (bold)
    • URL of an HTML <A> Link

Getting a textrange object

To get a textrange object you use the following techniques:

    • Get a textrange object that contains the current text selection, as described in getting a selection.
    • Create a textrange object that contains a specific range of text.

To create a textrange object with a specific range of text, use a textrange constructor with the following format:

 
New textrange (control, modifiesselection, beginindex, endindex)

Specify the control that contains the text, whether the textrange object corresponds to a selection (that is, represents and modifies selected text ), and the zero-based indexes in the text of the first and last character of the range. as a general rule, do not use the textrange constructor to set a selection; UseSetselection ()Method, as described in creating a selection. For this reason, the second parameter shoshould always beFalseWhen you specify the begin and end indexes.

To get a textrange object with the same th through twenty-character th characters of a textarea control named mytextarea, for example, use the following line:

VaR mytarange: textrange = new textrange (mytextarea, false, 4, 25 );

Changing text

After you get a textrange object, use its properties to modify the text in the range. The changes you make to the textrange appear in the text control.

You can get or set the text in a textrange object as HTML text or as a plain text, independent of any property that you might have used to initially set the text. if you created a textarea control, for example, and set itsTextProperty, you can use the textrangeHtmltextProperty to get and change the text. the following example shows this usage, and shows using the textrange class to access a range of text and change its properties. it also shows using string properties and methods to get text indexes.

<? XML version = "1.0"?> <! -- Textcontrols/textrangeexample. mxml --> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml"> <mx: SCRIPT> <! [CDATA [import MX. controls. textclasses. textrange private function resettext (): void {ta1.text = "this is a test of the emergency broadcast system. it is only a test. ";} public function altertext (): void {// create a textrange object starting with" the "and ending at the // first period. replace it with new formatted HTML text. vaR tr1: textrange = new textrange (TA1, false, ta1.text. indexof ("the", 0), ta1.text. indexof (". ", 0); tr1.htmltext =" <I> italic HTML text </I> "// create a textrange object with the remaining text. // select the text and change its formatting. vaR tr2: textrange = new textrange (TA1, true, ta1.text. indexof ("it", 0), ta1.text. length-1); tr2.color = 0xff00ff; tr2.fontsize = 18; tr2.fontstyle = "italic"; // any other value turns italic off tr2.fontweight = "bold "; // any other value turns bold off ta1.setselection (0, 0) ;}]]> </MX: SCRIPT> <mx: textarea id = "TA1" fontsize = "12" fontweight = "bold" width = "100%" Height = "100"> <mx: text> This is a test of the emergency broadcast system. it is only a test. </MX: Text> </MX: textarea> <mx: hbox> <mx: button label = "alter text" Click = "altertext ();"/> <mx: button label = "reset" Click = "resettext ();"/> </MX: hbox> </MX: Application>

The executing SWF file for the previous example is shown below:

Example: Changing selected text in a richtexteditor Control

The following example shows how you can useSelectedtextProperty of the richtexteditor control to get a textrange when a user selects some text, and use textrange properties to get and change the characteristics of the selected text. to use the example, select a range of text with your mouse. when you release the mouse button, the string "this is replacement text. ", formatted in fuchsia courier 20-point font replaces the selection and the text area reports on the original and replacement text.

<? XML version = "1.0"?> <! -- Textcontrols/textrangeselectedtext. mxml --> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" width = "600" Height = "500"> <mx: SCRIPT> <! [CDATA [import MX. controls. textclasses. textrange; // The following text must be on a single line. [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> "; Public Function changeselectiontext (): Void {// get a textrange with the selected text and find its length. vaR sel: textrange = rte1.selection; var sellength: Int = SEL. endindex-Sel. beginindex; // do the following only if the user made a selection. if (sellength) {// display the selection size and font color, size, and family. t1.text = "number of characters selected:" + String (sellength); t1.text + = "\ n \ noriginal font family:" + SEL. Fontfamily; t1.text + = "\ noriginal font size:" + SEL. fontsize; t1.text + = "\ noriginal font color:" + SEL. color; // change font color, size, and family and replace selected text. sel. TEXT = "this is replacement text. "SEL. color = "fuchsia"; SEL. fontsize = 20; SEL. fontfamily = "Courier" // show the new font color, size, and family. t1.text + = "\ n \ nNew text length:" + String (SEL. endindex-Sel. beginindex); t1.te XT + = "\ nNew font family:" + SEL. fontfamily; t1.text + = "\ nNew font size:" + SEL. fontsize; t1.text + = "\ nNew font color:" + SEL. color ;}}]> </MX: SCRIPT> <! -- The text area. when you release the mouse after selecting text, it callthe func1 function. --> <mx: richtexteditor id = "rte1" htmltext = "{htmldata}" width = "100%" Height = "100%" mouseup = "changeselectiontext ()"/> <mx: textarea editable = "false" id = "T1" fontsize = "12" fontweight = "bold" width = "300" Height = "180"/> </MX: Application>

The executing SWF file for the previous example is shown below:


Comments (1)
    • Lead biJanuary 20,200 9

      In the section "selecting text", shocould

      Mytextarea. endindex = 25;

      Be

      Mytextarea. selectionendindex = 25;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.