Skip to main content
Participating Frequently
Jun 3, 2010 7:50 AM UTC
Answered

Change style on selected text in TextFlow

Hi,

I try to figure this example out, but it only assume that a TextArea is used. I only got the TextFlow to work with. I've tried to translate it to only use functions from TextFlow, but with no success.

http://blog.flexexamples.com/2009/07/25/exporting-a-textflow-object-in-flex-4/ - (its the second example)

Thanks!

This topic is closed to new replies. Start a new post to keep the conversation going.
robin_briggsCorrect answer
Adobe Employee
Jun 7, 2010 4:05 AM UTC

I haven't tested it, but here's a quick sketch of what you need. Where they have:

protected function boldBtn_clickHandler(evt:MouseEvent):void {
                var txtLayFmt:TextLayoutFormat = editor.getFormatOfRange(null,
                                    editor.selectionAnchorPosition,
                                    editor.selectionActivePosition);
                txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD;
                editor.setFormatOfRange(txtLayFmt,
                                    editor.selectionAnchorPosition,
                                    .selectionActivePosition);
                editor.setFocus();
            }

You would have this:

protected function boldBtn_clickHandler(evt:MouseEvent):void {
                var txtLayFmt:TextLayoutFormat = new TextLayoutFormat();
                txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD;
                (textFlow.interactionManager as IEditManager).applyLeafFormat(txtLayFmt);
                }

- robin

4 replies

Adobe Employee
Jun 4, 2010 6:30 AM UTC

You can use the TextConverter class that's cited in the sample code, and just pass in the TextFlow object that you have.

So where it has:

 <s:Button id="htmlBtn"
                label="Export as HTML"
                click="debug.text = TextConverter.export(customEditor.editor.textFlow,
                                        TextConverter.HTML_FORMAT,
                                        ConversionType.STRING_TYPE).toString();" />
 
your equivalent click function would be:

debug.text = TextConverter.export(textFlow,
                                        TextConverter.HTML_FORMAT,
                                        ConversionType.STRING_TYPE).toString();

Hope this helps,

- robin

Translate
psan83Author
Participating Frequently
Jun 5, 2010 9:55 PM UTC

Hi and thanks for your answer, but I was talking about the second example on the link I gave. The one where a custom toolbar for a TextArea is created. For example to set bold text on a selected text, only using the textflow variable.

Translate
robin_briggsCorrect answer
Adobe Employee
Jun 7, 2010 4:05 AM UTC

I haven't tested it, but here's a quick sketch of what you need. Where they have:

protected function boldBtn_clickHandler(evt:MouseEvent):void {
                var txtLayFmt:TextLayoutFormat = editor.getFormatOfRange(null,
                                    editor.selectionAnchorPosition,
                                    editor.selectionActivePosition);
                txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD;
                editor.setFormatOfRange(txtLayFmt,
                                    editor.selectionAnchorPosition,
                                    .selectionActivePosition);
                editor.setFocus();
            }

You would have this:

protected function boldBtn_clickHandler(evt:MouseEvent):void {
                var txtLayFmt:TextLayoutFormat = new TextLayoutFormat();
                txtLayFmt.fontWeight = (txtLayFmt.fontWeight == FontWeight.BOLD) ? FontWeight.NORMAL : FontWeight.BOLD;
                (textFlow.interactionManager as IEditManager).applyLeafFormat(txtLayFmt);
                }

- robin

Translate