Flex Multiline Button

Flex Multiline Button

Posting mostly so I can find this again quickly.

UPDATE: posted working example (the code below does work). Right click – view source.

Basically stolen straight from Alex’s Flex Closet, but a button (not radio button), fixed casting, and just included the referenced class in the same file (less flexible, but, more convenient, maybe). Anyway – use just like a button but has multiple lines.

package components.multiLineButton
{
import flash.display.DisplayObject;
import flash.text.TextLineMetrics;

import mx.controls.Button;
import mx.core.IFlexDisplayObject;
import mx.core.mx_internal;
use namespace mx_internal;

public class MultiLineButton extends Button
{
public function MultiLineButton()
{
super();
}

override protected function createChildren():void
{
if (!textField)
{
textField = new NoTruncationUITextField();
textField.styleName = this;
addChild(DisplayObject(textField));
}

super.createChildren();

textField.multiline = true;
textField.wordWrap = true;
textField.width = width;
}

override protected function measure():void
{
if (!isNaN(explicitWidth))
{
var tempIcon:IFlexDisplayObject = getCurrentIcon();
var w:Number = explicitWidth;
if (tempIcon)
w -= tempIcon.width + getStyle("horizontalGap") + getStyle("paddingLeft") + getStyle("paddingRight");
textField.width = w;
}
super.measure();

}

override public function measureText(s:String):TextLineMetrics
{
textField.text = s;
var lineMetrics:TextLineMetrics = textField.getLineMetrics(0);
lineMetrics.width = textField.textWidth + 4;
lineMetrics.height = textField.textHeight + 4;
return lineMetrics;
}
}
}
import mx.core.UITextField;

class NoTruncationUITextField extends UITextField
{
public function NoTruncationUITextField()
{
super();
}
override public function truncateToFit(s:String = null):Boolean
{
return false;
}

}


转载:http://www.forestandthetrees.com/2008/03/11/flex-multiline-button/

你可能感兴趣的:(Flex Multiline Button)