Skip to main content
Participant
March 11, 2012
Question

StyleableTextField htmlText not working properly

  • March 11, 2012
  • 1 reply
  • 1232 views

Hi,

I am trying to customize the LabelItemRenderer so that I can use htmlText property. I used this CustomRenderer on my List component.

When the List loads, I can't see the effect of the bold and font colors tag but when I tried to scroll it I was able to see the effect of bold and font color tag.

How would I be able to see the effect of the bold and font colors without a need to scroll it before it take effect.

Here is the codes of the customize LabelItemRenderer :

package components

{

          import spark.components.HGroup;

          import spark.components.Label;

          import spark.components.LabelItemRenderer;

          import spark.components.supportClasses.StyleableTextField;

 

          public class CustomRenderer extends LabelItemRenderer

          {

                    public var hGroup:HGroup;

                    public var textLabel:StyleableTextField;

 

                    public function CustomRenderer()

                    {

                              super();

                    }

 

                    override public function set data(value:Object):void

                    {

                              super.data = value;

                              if (!value) return;

 

                              invalidateProperties();

                    }

 

                    override protected function createChildren():void {

                              if (!hGroup)

                              {

                                        hGroup = new HGroup();

                                        hGroup.paddingLeft = 5;

                                        hGroup.paddingRight = 5;

                                        hGroup.verticalAlign = "middle";

                                        hGroup.percentWidth = 100;

                                        addChild(hGroup);

                              }

                    }

 

                    override protected function commitProperties():void

                    {

                              super.commitProperties();

                              if (!textLabel) {

                                        textLabel = StyleableTextField(createInFontContext(StyleableTextField));

                                        textLabel.styleName = this;

                                        textLabel.selectable = false;

                                        textLabel.editable = false;

                                        textLabel.multiline = true

                                        textLabel.wordWrap = true;

 

                                        hGroup.addElement(textLabel);

                              }

                              textLabel.htmlText = "<b><font color='#00ff00'>" + data.TextToDisplay + "</font></b>";

 

                              invalidateDisplayList();

                              invalidateSize();

                    }

 

                    // Override layoutContents() to lay out the HGroup container.

                    override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void

                    {

                              // Make sure our width/height is in the min/max for the label

                              var childWidth:Number = unscaledWidth - 6;

                              childWidth = Math.max(hGroup.getMinBoundsWidth(), Math.min(hGroup.getMaxBoundsWidth(), childWidth));

                              var childHeight:Number = unscaledHeight - 10;

                              childHeight = Math.max(hGroup.getMinBoundsHeight(), Math.min(hGroup.getMaxBoundsHeight(), childHeight));

 

                              // Set the label's position and size

                              hGroup.setLayoutBoundsSize(childWidth, childHeight);

                              hGroup.setLayoutBoundsPosition(3, 5);

 

                              textLabel.commitStyles();

                    }

          }

}

Here is the code of  the View:

<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

                    xmlns:s="library://ns.adobe.com/flex/spark" title="Test"

                    viewActivate="viewAct()" initialize="view1_initializeHandler(event)">

          <fx:Script>

                    <![CDATA[

                              import components.CustomRenderer;

 

                              import mx.collections.ArrayList;

                              import mx.events.FlexEvent;

 

                              [Bindable] private var arrList:ArrayList = new ArrayList();

 

                              private function viewAct():void

                              {

                                        var factory:ClassFactory = new ClassFactory(CustomRenderer);

                                        myList.itemRenderer = factory;

                              }

 

                              protected function view1_initializeHandler(event:FlexEvent):void

                              {

                                        // TODO Auto-generated method stub

                                        var obj:Object;

 

                                        for(var i:Number = 0; i < 10; i++)

                                        {

                                                  obj = new Object();

                                                  obj.TextToDisplay = "hello" + i.toString();

 

                                                  arrList.addItem(obj);

                                        }

                              }

 

                    ]]>

          </fx:Script>

          <fx:Declarations>

                    <!-- Place non-visual elements (e.g., services, value objects) here -->

          </fx:Declarations>

 

          <s:layout>

                    <s:VerticalLayout/>

          </s:layout>

 

          <s:List id="myList" width="100%" height="100%" dataProvider="{arrList}" contentBackgroundColor="white"/>

</s:View>

Can someone help me please?

Thanks in advanced.

This topic has been closed for replies.

1 reply

Inspiring
March 18, 2012

Hi,

For performance, don't use addItem : use a temporary array and an ArrayCollection.

I don't know if your problem for html is complex but for this simple example (with bold and color), use a List like this :

<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        xmlns:c="components.*"       

        title="List"   

        viewActivate="viewActivate()"

    >   

    <fx:Declarations>

        <s:ArrayCollection id="dataAC" />

    </fx:Declarations>

    <fx:Script>

        <![CDATA[

            public function viewActivate():void

            {       

                var obj:Object;

                var list:Array = [];

                for(var i:int = 0; i < 40; i++)

                {

                    obj = new Object();

                    obj.TextToDisplay = "hello " + i.toString();

                    list.push(obj);               

                }

                dataAC.source = list;

            }

           

        ]]>

    </fx:Script>

    <s:List id="myList" width="100%" height="100%"

            dataProvider="{dataAC}"

            labelField="TextToDisplay"

            fontWeight="bold"

            color="0x00ff00"

            alternatingItemColors="[0xffffff, 0xeeeeee]"

            />

</s:View>

Philippe

xineo7Author
Participant
March 19, 2012

Hi Philippe,

Thank you for the tip that would be really helpful. I found a solution on my problem but Im not so sure about it because I need to include this code on my CustomRenderer.as for the htmlTag to render properly :

  var styles:String = "";

                                        var myStyleSheet:StyleSheet = new StyleSheet();

                                        myStyleSheet.parseCSS(styles);

 

                                        textLabel.styleSheet = myStyleSheet;

By the way I used the htmlText property because some of the text should be on different color like this example -> <font color="#ff0000">h</font><font color="#00ff00">e</font>

Again thank you.

Martin