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
- 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.
- 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.
Mrinal Wadhwa said
Welcome to the Flex blogosphere!
Subbu said
All the best, Rashmi…
Happy Flexing..!
Arul said
Welcome on-board
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”;
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.
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
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)
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!!
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!
Russell Evanson said
Hey.
Could you send me a copy of the code please. My email address is russellevanson@innovate-software.co.uk. Thanks.
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.
Edu man said
oh, thanks for sharing
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
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
Vasudev said
Hi,
I’m new to Flex, Could you please send me the sample for multiline buttons.
Thanks
Vasudev
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
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
Manu said
Can u please post me the code (working code). Thanks.
Manu said
Can u please post me the code (working code). Thanks. manuraj.dhanda@gmail.com
Gabriela Trindade Perry: HCI, Ergonomia, Flex » Blog Archive » Multiline Button said
[...] 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 [...]
Gabriela Trindade Perry said
I published the Button extended class, with a link for this post.
Thank you so much for these tips :0)
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;
}
}
}
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
Umer said
this is not a problem now
http://flexlib.googlecode.com/svn/trunk/docs/flexlib/controls/CanvasButton.html
Very simple
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;
}
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;
}
}
}
Shane said
Works brilliantly, thanks so much!
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.
anon said
I think you all are over-complicating this. Just use:
mx.Controls.Text
The Text component is multi-line. Problem solved.
gaurav said
Can you please post the example for adding double line label for TAB NAVIGATOR.
Thanks.
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;
}
jambo13 said
Thanks Darren, that example works for me