Skip to main content
Inspiring
August 3, 2010
Question

How to adjust a div position?

  • August 3, 2010
  • 2 replies
  • 1505 views

Well, here is my attempt to adjust a div container but discovered I can't apply any styles that are declared.  Can someone please take look.

Is it ok for me to convert 'data' as TEXT_LAYOUT_FORMAT?

Why aren't CSS styles being applied?

And to my orginal question, how can I adjust/justify/move a div element?

<?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="500" minHeight="500" creationComplete="application1_creationCompleteHandler(event)">
     <fx:Style>
          @namespace s "library://ns.adobe.com/flex/spark";
          @namespace mx "library://ns.adobe.com/flex/mx";
          
          .body
          {
               font-size: 18;     
          }
          
          .divRight
          {
               padding-left:      100;
               padding-right:      100;
          }
     </fx:Style>
     
     <fx:Script>
          <![CDATA[
               import flashx.textLayout.compose.StandardFlowComposer;
               import flashx.textLayout.container.ContainerController;
               import flashx.textLayout.container.ScrollPolicy;
               import flashx.textLayout.conversion.TextConverter;
               import flashx.textLayout.elements.TextFlow;
               
               import mx.events.FlexEvent;
               
               import spark.core.SpriteVisualElement;
               
               
               private var textFlow:TextFlow;
               
               private var xml:XML =           <flow>
                                                          <div>
                                                                 <p id='body'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>    
                                                          </div>
                                                          <div id='divRight'>
                                                                 <p>Hello.</p>
                                                          </div>
                                                    </flow>
               
               protected function application1_creationCompleteHandler(event:FlexEvent):void
               {
                    convertXMLToTextFlow(xml);
               }

               private function convertXMLToTextFlow(xml:XML):void
               {
                    var sprite:SpriteVisualElement              = new SpriteVisualElement();
                    var container:ContainerController           = new ContainerController(sprite, 500, 500);
                    
                    var data:String = "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008'>"+xml.children()+"</TextFlow>";
                    
                    textFlow = TextConverter.importToFlow(data, TextConverter.TEXT_LAYOUT_FORMAT);
                    textFlow.flowComposer                     = new StandardFlowComposer();
                    textFlow.formatResolver                     = new CSSFormatResolverTLF();
                    textFlow.flowComposer.addController(container);                   
                    
                    group.addElement(sprite);

                    textFlow.flowComposer.updateAllControllers();
               }
          ]]>
     </fx:Script>
     
     <s:Group id="group"/>

</s:Application>

This topic has been closed for replies.

2 replies

Adobe Employee
August 5, 2010

Flex doesn't support apply CSS styles to TLF elements, unfortunately. In order to get the proerty to apply, you have to set it directly on the <div> element, as in div.paddingRight = 100;


Hope this helps,

- robin

injpixAuthor
Inspiring
August 5, 2010

You mentioned, "...you have to set it directly on the <div>...".  In other words, set it in the XML?  Like this:

<div styleName="divRight" paddingRight='100'/>

Or to seek the DivElement and assign a paddingRight value to it, as you posted.  For an example:

var divRightTags:Array = textFlow.getElementsByStyleName("divRight");
for each(var divElement:FlowGroupElement in divRightTags)
{
    divElement.paddingRight = 100;
}

At any rate, I did both methods and the DivElement stayed in the same location.  That is, it justified to the far left.  And I did, in an addition to the two methods added a paddingLeft of the same value; same results.

Adobe Employee
August 5, 2010

Being able to set padding on the <div> is a TLF 2.0 feature (currently in prerelease). If you have new code from our SourceForge site, this should work. You can either set it on the element in the XML, and then import it, or set it directly on the DivElement once it's been created. Are you using TLF 2.0, or the TLF that comes with the Flex SDK?

If you are using TLF 1.0 or 1.1, I'd suggest setting the indent on the paragraphs in the <div>. Semantically different, but you can get the exact same display on screen. See paragraphStartIndent and paragraphEndIndent for paddingLeft and paddingRight.

Thanks,

- robin

injpixAuthor
Inspiring
August 4, 2010

I am now able to "move" a div left or right by assigning it 2 columns.  However I am unable to answer my first 2 questions:

Is it ok for me to convert 'data' as TEXT_LAYOUT_FORMAT?

Why aren't CSS styles being applied?

Below is my updated sample showing how to "move" the div.  This works well with InlineGraphicElement too.

<?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="500" minHeight="500" creationComplete="application1_creationCompleteHandler(event)">
     <fx:Style>
          @namespace s "library://ns.adobe.com/flex/spark";
          @namespace mx "library://ns.adobe.com/flex/mx";
          
          .body
          {
               font-size: 18;     
          }
          
     </fx:Style>
     
     <fx:Script>
          <![CDATA[
               import flashx.textLayout.compose.StandardFlowComposer;
               import flashx.textLayout.container.ContainerController;
               import flashx.textLayout.container.ScrollPolicy;
               import flashx.textLayout.conversion.TextConverter;
               import flashx.textLayout.elements.FlowGroupElement;
               import flashx.textLayout.elements.TextFlow;
               import flashx.textLayout.formats.Direction;
               import flashx.textLayout.formats.TextLayoutFormat;
               
               import mx.events.FlexEvent;
               
               import spark.core.SpriteVisualElement;
               
               
               private var textFlow:TextFlow;
               
               private var xml:XML =           <flow>
                                                          <div>
                                                                 <p id='body'>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>    
                                                          </div>
                                                          <div styleName='divRight'>
                                                                 <p>Hello.</p>
                                                          </div>
                                                    </flow>
               
               protected function application1_creationCompleteHandler(event:FlexEvent):void
               {
                    convertXMLToTextFlow(xml);
               }

               private function convertXMLToTextFlow(xml:XML):void
               {
                    var sprite:SpriteVisualElement              = new SpriteVisualElement();
                    var container:ContainerController           = new ContainerController(sprite, 500, 500);
                    
                    var data:String = "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008'>"+xml.children()+"</TextFlow>";
                    
                    textFlow = TextConverter.importToFlow(data, TextConverter.TEXT_LAYOUT_FORMAT);
                    textFlow.flowComposer                     = new StandardFlowComposer();
                    textFlow.formatResolver                     = new CSSFormatResolverTLF();
                    textFlow.flowComposer.addController(container);                   
                    
                    splitDivElements();
                    
                    group.addElement(sprite);

                    textFlow.flowComposer.updateAllControllers();
               }
               
               private function splitDivElements():void
               {
                    var divRightTags:Array = textFlow.getElementsByStyleName("divRight");
                    for each(var divElement:FlowGroupElement in divRightTags)
                    {
                         divElement.columnCount = 2;
                         divElement.columnWidth = 250;
                         divElement.direction = Direction.RTL;
                    }
               }
          ]]>
     </fx:Script>
     
     <s:Group id="group"/>

</s:Application>