Flex: Label.addChild () not working?

I want to put a caption above it, for example, so that the label says $ 1000 with a small retail price or our price higher. I am trying to add a title label to the main label display list. I don't get any error, but the title is not displayed. I've looked at rawChildren as well, but obviously Label doesn't have a rawChildren property.

Here is my code:

package
{
    import mx.controls.Label;

    public class PriceLabel extends StrikeThroughLabel //<-- exntension of label to add strike
    {
        private var _title:Label;

        public function PriceLabel()
        {
            super();
        }

        [Bindable]
        public function set title(s:String):void
        {
            if(_title == null)
            {
                _title = new Label();
                addChild(_title);
                this.alpha = .2;
            }
            _title.text = s;
        }
        public function get title():String
        {
            var s:String
            if(_title != null)
            {
                s = _title.text;
            }
            return s;
        }

    }
}

      

+1


a source to share


2 answers


If you are adding children to a Flex component that is not a container, you need to manually control the size and position of those children. Containers do a lot of work for you.

Here's what you should do:

  • Move the creation of your child shortcut to a function override createChildren()

    .

  • Set the text

    child label property in the function override commitProperties()

    . Your getter and setter title

    must store the value in a variable _title

    that will be used later for assignment to commitProperties()

    . This is really important for performance.

  • Override the function measure()

    and update measuredWidth

    and measuredHeight

    to be the maximum width and height values ​​for the main label and its child.

  • Undo updateDisplayList()

    and use setActualSize()

    on the child label to set it to your desired width and height.



This may sound like a lot of work, but in terms of best practices, how should you create custom components. The Flex team at Adobe has spent a lot of time getting the most out of it, and so everything happens in stages.

How to do it based on what you asked for. Personally, I would subclass UIComponent with two labels, or UITextFields as children, each with its own separate property.

By the way, this property rawChildren

is only available for containers. It exists, so you can add chrome to the container that is not part of the container layout's child layout algorithm. For example, a panel has a title bar and a border that are not affected by the vertical / horizontal / absolute layout options.

+2


a source


Why not create a custom component that contains both labels as its children, instead of trying to throw the child on the label? This seems cleaner to me as adding children to components like this seems to be wrong.



+1


a source







All Articles