Skip to main content
Inspiring
May 14, 2010
Answered

Links loose style when text style has been modified previously

  • May 14, 2010
  • 2 replies
  • 1327 views

I am making a custom RTE and have a method that inserts a hype link into the text area.  I have found that this works fine, except when I have modified the text in someway (i.e font size, wieght etc) then the link style does not appear.  The link is there, it is clickable when holding down the ctrl key.  Is this a bug, or does anyone have a way of fixing this?  Here is some simple code that I get the same wierdness from as well.  I just applied the font changes to the textArea in the creation complete, type some text, highlight it and click the button to apply a google.com link, and no blue underline style occurs:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*" creationComplete="onCreationComplete()">
    <fx:Script>
        <![CDATA[
            import flash.text.engine.FontWeight;
           
            import flashx.textLayout.edit.IEditManager;
            import flashx.textLayout.formats.TextLayoutFormat;
           
            import mx.events.FlexEvent;
           
           
            protected function onCreationComplete():void
            {
                var tf:TextLayoutFormat = blah2.getFormatOfRange(null,0,0);
                tf.fontSize = 16;
                tf.fontWeight = FontWeight.BOLD;
                blah2.setFormatOfRange(tf,0,0);
            }
           
            protected function button4_clickHandler(event:MouseEvent):void
            {
                var editManager:IEditManager = blah2.textFlow.interactionManager as IEditManager;
                editManager.applyLink('www.google.com');
            }
           
           
        ]]>
    </fx:Script>
   
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

   
    <s:TextArea  id="blah2"/>
   
    <s:Button  x="164" y="249" label="Button" click="button4_clickHandler(event)"/>
</s:Application>

This topic has been closed for replies.
Correct answer robin_briggs

I am wondering if the link formats are getting replaced when you do the setFormatOfRange call? I will ask the Flex team about this.

In the meantime, I rewrote your creationComplete handler in TLF calls, and it seems to work. Try this:

protected function onCreationComplete():void

{

var tf:TextLayoutFormat = new TextLayoutFormat;

tf.fontSize = 16;

tf.fontWeight = FontWeight.BOLD;

var textFlow:TextFlow = TextFlow(blah2.content);

var editManager:IEditManager = IEditManager(textFlow.interactionManager);

editManager.selectRange(0, textFlow.textLength);

editManager.applyFormat(tf, null, null);

textFlow.flowComposer.updateAllControllers();

}

- robin

2 replies

Participating Frequently
May 17, 2010

To use Flex, replace

    var tf:TextLayoutFormat = blah2.getFormatOfRange(null,0,0);

with

     var tf:TextLayoutFormat = new TextLayoutFormat;

I will need to work out with Robin why the first one doesn't work.

hanchan07Author
Inspiring
May 17, 2010

Thanks for looking into this guys.  Both of these solutions seem to work, but only as long as the text has not been modified again.

For Example once it has loaded I can type '123456' then if I change the font wieght to bold and add 'abcdefg' so the text in my text area looks like: 123456abcdefg

Selecting the 'abcdefg' text and creating a link with editManager.applyLink('www.google.com'); does not apply the link style, but if I then highlight the 123456 and create a link the style is there.  So it does look like something is getting lost.  I'll keep playing with it and see what I can come up with.

Adobe Employee
May 17, 2010

I looked into this a little more. The short answer is that when you are changing the format, you should change only those items you are actually setting, and not initialize using getFormatOfRange. The longer answer is that getFormatOfRange is returning the computed value of every property, which means (for example) taking defaults into account. When you take the result of this, and apply it back to the text, using setFormatOfRange, the span ends up having values for color and textDecoration that override those that are applied at the link level. Therefore there is no visual indication of the link.

So, I recomment *not* doing this:

                var tf:TextLayoutFormat = blah2.getFormatOfRange(null,0,0);
                tf.fontSize = 16;
                tf.fontWeight = FontWeight.BOLD;
                blah2.setFormatOfRange(tf,0,0);

Instead, use this pattern:

                var tf:TextLayoutFormat = new TextLayoutFormat();
                 tf.fontSize = 16;
                 tf.fontWeight = FontWeight.BOLD;
                 blah2.setFormatOfRange(tf,0,0);

If you are having problems in other cases where the text has been changed, can you check that you are not taking getFormatOfRange and using the resultant format in a call to setFormatOfRange? If you are still having problems, please post the code and I'll take a look.

Thanks,

- robin

robin_briggsCorrect answer
Adobe Employee
May 17, 2010

I am wondering if the link formats are getting replaced when you do the setFormatOfRange call? I will ask the Flex team about this.

In the meantime, I rewrote your creationComplete handler in TLF calls, and it seems to work. Try this:

protected function onCreationComplete():void

{

var tf:TextLayoutFormat = new TextLayoutFormat;

tf.fontSize = 16;

tf.fontWeight = FontWeight.BOLD;

var textFlow:TextFlow = TextFlow(blah2.content);

var editManager:IEditManager = IEditManager(textFlow.interactionManager);

editManager.selectRange(0, textFlow.textLength);

editManager.applyFormat(tf, null, null);

textFlow.flowComposer.updateAllControllers();

}

- robin