Skip to main content
Participating Frequently
June 3, 2010
Answered

Change style on selected text in TextFlow

  • June 3, 2010
  • 1 reply
  • 1022 views

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 has been closed for replies.
Correct answer robin_briggs

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

1 reply

Adobe Employee
June 4, 2010

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

psan83Author
Participating Frequently
June 5, 2010

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.

robin_briggsCorrect answer
Adobe Employee
June 7, 2010

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