Flex, ActionScript and Related Stuffs

Enjoy!!!!

MultiLined Label for a Button

Posted by flexrays on April 24, 2007

I found this task very tough when I started it. But Mr. Alex Hauri’s blog made me beleive that it is not that difficult. I published it mainly for my reference even though it is a kind of repition of that blog. I hope that this will definitely help someone.

You can find Alex Hauri’s blog here http://blogs.adobe.com/aharui/

This is how it is done. You need to create a customized button by extending the Button class. The button class overrides the createChildren method and creates a UI textfield to display the label.

These are somethings which you need to know before you start with the task

  1. Button uses an instance of UITextField to render its label, so we need to create an instance of UITextField for the label on our customized button.
  2. Lable by default accommadates only a single line of text. In order to make it multiline, you need to override the createChildren() method of the Button class and give multiline and wordwrap behaviour to the instance of the textField and add it as a child of the button.

Please find the createChildren() as below.

override protected function createChildren():void
{
// Create a UITextField to display the label.
if (!textField)
{
textField = new UITextField();
textField.styleName = this;
addChild(textField);
}
super.createChildren();
textField.multiline = true;
textField.wordwrap = true;
}

My task wasnt that simple. I had to add multiline label to the Tab in the TabNavigator. And that label should display every word of the text in a different line. I took almost four days to figure this out as I am new to Flex development. But I have learnt a lot in those four days.

32 Responses to “MultiLined Label for a Button”

  1. Welcome to the Flex blogosphere!

  2. Subbu said

    All the best, Rashmi…

    Happy Flexing..!

  3. Arul said

    Welcome on-board :)

  4. Timo said

    Hi!

    Where do I need to put that code? I put it in between -tags and it says “Error: Access of undefined property textField.
    textField.wordwrap = true”;

  5. flexrays said

    Hi Timo.
    I am not able to upload the source file for Multi-lined button for some reason. So. Let me explain you a bit. You need to extend the Button Class and create your own customizedButton Class. and In the same class you need to override the createChildren() method and put my code into that. I can send the files if you leave your email ID.

  6. pankaj said

    i tried this code but not getting wrapping or multiline of label of button could u send me u r code my email id is pankaj_davv@yahoo.com

  7. ilogyc said

    Hi all!
    try to modify this property after the last line:

    textField.autoSize = TextFieldAutoSize.LEFT;

    (import flash.text.TextFieldAutoSize Class is required to use TextFieldAutoSize.LEFT constant)

  8. Danny Murong said

    Hi,
    Thanks for this post! I am still relatively new to Flex as well.

    You guys have offered great help!

    I have an issue, after I got the multiline working, I seen my multiline button label displayed from the middle of my button therefore my label text is longer that my button itself, is there a way to shift the text up higher to the top of the button?

    Am i suppose to extend the updateDisplayList() in Button class?

    Please advise!! thanks!!

  9. Amazed! said

    It’s incredible something like having a button with wordwrapped text is so complicated! In .NET it just works obviously, put the text in, size button heigh so there is enough room for the wrap, and it just does it! None of this child controls stuff!

  10. Russell Evanson said

    Hey.
    Could you send me a copy of the code please. My email address is russellevanson@innovate-software.co.uk. Thanks.

  11. jwopitz said

    Have you run into any issues with using your class in a repeater? I had made a custom class at one point that contained a non-public UITextField member.

    Anytime you had a binding mechanism update the dataProvider to the repeater, it would through a run-time-error saying that the UITextField was not an IRepeaterClient even though the UITextField was non-public.

    I sent a bug report to Adobe with no response. I ultimately just made an extension of UITextField that implements IRepeaterClient until the issue is officially addressed.

    What gets me about this is that you can use any of the mx controls, many of which containe non-public instances of UITextField, in a repeater with no issue. Anyway, I hope that helps someone.

  12. Edu man said

    oh, thanks for sharing

  13. Estakmoza said

    Hello,
    I’m also new to Flex, and I was looking for a way to enable multilined buttons for a label.

    Can you please, send me the sample code to: estakmoza@gmail.com

    Thanks,

    Ahmad

  14. Nick said

    Hey, I’ve been trying to figure out how to work this, and where exactly to put the code, but failed every time. Could you send me a copy of the code for the multi-lined button? Thanks, -Nick.
    My email is nickel_the_first@hotmail.com

  15. Vasudev said

    Hi,

    I’m new to Flex, Could you please send me the sample for multiline buttons.

    Thanks
    Vasudev

  16. Jameel said

    Hi All,
    Did anyone have the working code for the multiline label? I need to do it for Tabnavigator :-(

    help needed guys !!!!

    many thanks in advance

    Regards
    Jameel

  17. RSriram said

    Can you please send me the code. I have tried MultilineButton but does not work for me. The text appears in one line
    rakhi_sriram@yahoo.com

  18. Manu said

    Can u please post me the code (working code). Thanks.

  19. Manu said

    Can u please post me the code (working code). Thanks. manuraj.dhanda@gmail.com

  20. [...] also: http://flexrays.wordpress.com/2007/04/24/multilined-label-for-a-button/ and http: blogs.adobe.com/aharui/2007/04/multiline_buttons.html Both are great references. package [...]

  21. I published the Button extended class, with a link for this post.
    Thank you so much for these tips :0)

  22. MJ said

    To get this working in Flex 3.0…you need to add the line that Ilogyc referenced above, and you need to cast the textField before adding it to the container. Hope it helps…

    package xxx
    {
    import flash.display.DisplayObject;

    import mx.controls.Button;
    import mx.core.UITextField;
    importflash.text.TextFieldAutoSize;

    public class MultiLineButton extends Button
    {
    override protected function createChildren():void {
    // Create a UITextField to display the label.
    if (!textField)
    {
    textField = new UITextField();
    textField.styleName = this;
    addChild(DisplayObject(textField));
    }
    super.createChildren();
    textField.multiline = true;
    textField.wordWrap = true;
    textField.autoSize = TextFieldAutoSize.LEFT;
    }
    }
    }

  23. prica said

    To all of you wondering, the correct code goes like this:

    override protected function createChildren():void
    {
    // Create a UITextField to display the label.
    if (!textField)
    {
    textField = new UITextField();
    textField.styleName = this;
    addChild(DisplayObject(textField));
    }
    super.createChildren();
    textField.multiline = true;
    textField.wordWrap = true;
    }

    Cheers,
    prica

  24. Umer said

    this is not a problem now

    http://flexlib.googlecode.com/svn/trunk/docs/flexlib/controls/CanvasButton.html

    Very simple

  25. Darren said

    I also needed to override updateDisplayList as the top of the UITextField was in the centre of the button, not the middle of the TextField:

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    {
    super.updateDisplayList(unscaledWidth, unscaledHeight);
    textField.y = (this.height-textField.height)>>1;
    }

  26. Darren said

    ok, replacing the textField with the example above plays some havoc if you start embedding fonts into your app. I found a much easier way to make the textField in a button multiline capable. Basically include the mx_internal namespace and set the multiline property on the protected textField to true.

    package …
    {
    import flash.text.TextFieldAutoSize;
    import mx.controls.Button;
    import mx.core.mx_internal;

    use namespace mx_internal;

    public class MultilineButton extends Button
    {

    public function MultilineButton()
    {
    super();
    }

    override protected function createChildren():void
    {
    super.createChildren();
    textField.multiline = true;
    textField.wordWrap = true;
    textField.autoSize = TextFieldAutoSize.LEFT;
    }

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
    super.updateDisplayList(unscaledWidth, unscaledHeight);
    textField.y = (this.height-textField.height)>>1;
    }
    }
    }

  27. Shane said

    Works brilliantly, thanks so much!

  28. Sunil said

    How can i apply the same for TabNavigator or TabBar? It looks like extending the TabBar will not cut it, which class is extends the Button in the Tab* controls? Thanks Much In Advance.

  29. anon said

    I think you all are over-complicating this. Just use:

    mx.Controls.Text

    The Text component is multi-line. Problem solved.

  30. gaurav said

    Can you please post the example for adding double line label for TAB NAVIGATOR.

    Thanks.

  31. Gautam Bhandarkar said

    just one thing.
    one must override updateDisplayList method and specify height and width for textField to reflect multiline label button

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    {
    super.updateDisplayList(unscaledWidth, unscaledHeight);
    textField.y = (this.height-textField.height)+23>>1;
    //textField.y =15;

    textField.height=30;
    textField.width=65;
    }

  32. jambo13 said

    Thanks Darren, that example works for me :-D

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>