Skip to content

Instantly share code, notes, and snippets.

@codeinvain
Created June 22, 2010 21:00
Show Gist options
  • Save codeinvain/449065 to your computer and use it in GitHub Desktop.
Save codeinvain/449065 to your computer and use it in GitHub Desktop.
// Flex 3.x true RTL support
// this class depends on text layout framework
// you can download it from http://labs.adobe.com/technologies/textlayout/ and follow installation instructions
package ui
{
import flashx.textLayout.container.DisplayObjectContainerController;
import flashx.textLayout.elements.ParagraphElement;
import flashx.textLayout.elements.SpanElement;
import flashx.textLayout.elements.TextFlow;
import mx.core.FlexSprite;
import mx.core.UIComponent;
[Bindable]
public class ExtendedLabel extends UIComponent
{
private var container:FlexSprite;
private var span:SpanElement;
private var _text:String;
public function ExtendedLabel()
{
}
public function set text(value:String):void
{
_text = value;
invalidateProperties();
}
public function get text():String
{
return _text;
}
private var _multiLine:Boolean;
public function set multiLine(value:Boolean):void
{
_multiLine = value;
}
public function get multiLine():Boolean
{
return _multiLine;
}
protected override function commitProperties():void
{
drawText(_text);
super.commitProperties();
}
private var _fontFamily:String;
public function set fontFamily(value:String):void
{
_fontFamily = value;
invalidateProperties();
}
public function get fontFamily():String
{
if(isNullOrEmpty(_fontFamily))
return this.getStyle("font-family");
return _fontFamily;
}
private var _fontWeight:String;
public function set fontWeight(value:String):void
{
_fontWeight = value;
}
public function get fontWeight():String
{
if(isNullOrEmpty(_fontWeight))
return this.getStyle("font-weight");
return _fontWeight;
}
private var _fontSize:Object;
public function set fontSize(value:Object):void
{
_fontSize = value;
invalidateProperties();
}
public function get fontSize():Object
{
if(isNullOrEmpty(_fontSize))
return this.getStyle("font-size");
return _fontSize;
}
private var _color:Object;
public function set color(value:Object):void
{
_color = value;
invalidateProperties();
}
public function get color():Object
{
if(isNullOrEmpty(_color))
return this.getStyle("color");
return _color;
}
private var _paddingLeft:Object;
public function set paddingLeft(value:Object):void
{
_paddingLeft = value;
invalidateProperties();
}
public function get paddingLeft():Object
{
if(isNullOrEmpty(_paddingLeft))
return this.getStyle("padding-left");
return _paddingLeft;
}
private var _paddingRight:Object;
public function set paddingRight(value:Object):void
{
_paddingRight = value;
invalidateProperties();
}
public function get paddingRight():Object
{
if(isNullOrEmpty(_paddingRight))
return this.getStyle("padding-right");
return _paddingRight;
}
private var _paddingTop:Object;
public function set paddingTop(value:Object):void
{
_paddingTop = value;
invalidateProperties();
}
public function get paddingTop():Object
{
if(isNullOrEmpty(_paddingTop))
return this.getStyle("padding-top");
return _paddingTop;
}
private function drawText(str:String):void
{
if (container!=null)
{
this.removeChildAt(0);
container = null;
}
container = new FlexSprite();
var textFlow:TextFlow = new TextFlow();
var p:ParagraphElement = new ParagraphElement();
p.textAlign = "right";
p.direction = "rtl";
p.fontFamily = fontFamily;
p.fontWeight = fontWeight;
p.fontSize = fontSize;
p.color = color;
textFlow.addChild(p);
textFlow.paddingTop = paddingTop;
textFlow.paddingRight = paddingRight;
if(!this.multiLine)
textFlow.lineHeight = this.height;
span = new SpanElement();
span.text = str;
p.addChild(span);
textFlow.flowComposer.addController(new DisplayObjectContainerController(container, this.width, this.height));
textFlow.flowComposer.updateAllContainers();
addChild(container);
}
private function isNullOrEmpty(obj:Object):Boolean
{
return (obj == "" || obj == null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment