I'm trying to get the effect of a gradient fill for text. I figured - draw a gradient rect, then mask it w/ text.
I'm having a difficult time masking a UIComponent (could be anything really) with a Text component. The following works great in Flash (provided you embed the font of course).
ActionScript Code:
var tf:TextField = new TextField();
tf.text = "GRADIENT TEXT";
tf.embedFonts = true;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.setTextFormat( new TextFormat( "Arial", 20, 0x000000 ) );
var rect:Sprite = new Sprite();
var matrix:Matrix = new Matrix();
matrix.createGradientBox( 100, 20, Math.PI / 2 );
rect.graphics.beginGradientFill( GradientType.LINEAR, [ 0x000000, 0xffffff, 0x000000 ], [1,1,1], [0, 130, 255], matrix );
rect.graphics.drawRect( 0, 0, tf.width, tf.height );
rect.graphics.endFill();
rect.mask = tf;
addChild( rect );
addChild( tf );
Here is what I was trying to do in Flex - w/ no success:
ActionScript Code:
var textMask:Text = new Text();
textMask.text = "GRADIENT TEXT";
textMask.setStyle( "color", 0x000000 );
textMask.setStyle( "fontFamily", "Arial" );
textMask.setStyle( "fontWeight", "Bold" );
textMask.setStyle( "fontSize", 15 );
textMask.selectable = false;
var gradientRect:UIComponent = new UIComponent();
gradientRect.cacheAsBitmap = true;
var g:Graphics = gradientRect.graphics;
var matrix:Matrix = new Matrix();
matrix.createGradientBox( 100, 20, Math.PI / 2 );
g.beginGradientFill( GradientType.LINEAR, [ 0x000000, 0xffffff, 0x000000 ], [1,1,1], [0, 130, 255], matrix );
g.drawRect( textMask.x, textMask.y, this.width, this.height );
g.endFill();
gradientRect.mask = textMask;
addChild( gradientRect );
I first checked to make sure that the gradient was getting drawn and that its width and height were correct. I also checked to ensure that the text was getting placed correctly.
I need to double-check I guess that the embedded font Arial is in fact getting used - it is elsewhere in the app.
Any help here will be GREATLY appreciated.
Thanks,
B
Brandon E.B. Ward
UX & UI Designer, Flex & Flash Developer
http://www.uxd.me