Ultrashock Forums > Flash > Flex
Flex - Text as Mask ... not working

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
Flex - Text as Mask ... not working
Old 2009-01-16

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:
  1. var tf:TextField = new TextField();
  2. tf.text = "GRADIENT TEXT";
  3. tf.embedFonts = true;
  4. tf.autoSize = TextFieldAutoSize.LEFT;
  5. tf.setTextFormat( new TextFormat( "Arial", 20, 0x000000 ) );
  6.  
  7. var rect:Sprite = new Sprite();
  8. var matrix:Matrix = new Matrix();
  9. matrix.createGradientBox( 100, 20, Math.PI / 2 );
  10. rect.graphics.beginGradientFill( GradientType.LINEAR, [ 0x000000, 0xffffff, 0x000000 ], [1,1,1], [0, 130, 255], matrix );
  11. rect.graphics.drawRect( 0, 0, tf.width, tf.height );
  12. rect.graphics.endFill();
  13.  
  14. rect.mask = tf;
  15.  
  16. addChild( rect );
  17. addChild( tf );

Here is what I was trying to do in Flex - w/ no success:

ActionScript Code:
  1. var textMask:Text = new Text();
  2. textMask.text = "GRADIENT TEXT";
  3. textMask.setStyle( "color", 0x000000 );
  4. textMask.setStyle( "fontFamily", "Arial" );
  5. textMask.setStyle( "fontWeight", "Bold" );
  6. textMask.setStyle( "fontSize", 15 );
  7. textMask.selectable = false;
  8.  
  9. var gradientRect:UIComponent = new UIComponent();
  10. gradientRect.cacheAsBitmap = true;
  11.  
  12. var g:Graphics = gradientRect.graphics;
  13. var matrix:Matrix = new Matrix();
  14. matrix.createGradientBox( 100, 20, Math.PI / 2 );
  15.  
  16. g.beginGradientFill( GradientType.LINEAR, [ 0x000000, 0xffffff, 0x000000 ], [1,1,1], [0, 130, 255], matrix );
  17. g.drawRect( textMask.x, textMask.y, this.width, this.height );
  18. g.endFill();
  19.  
  20. gradientRect.mask = textMask;
  21.  
  22. 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
postbit arrow 3 comments | 1665 views postbit arrow Reply: with Quote   
Registered User
wraevn is offline
seperator
Posts: 434
2003-11-14
Age: 33
wraevn lives in United States
wraevn's Avatar
seperator

Ultrashock Member Comments:
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2009-01-16 #2 Old  
Here you go. I embed the Arial font into my library and just took a bitmapdata screen shot of it. Not sure if there is a cleaner way but this does work

Code:
var arial:Font = new ArialFont();

var format:TextFormat = new TextFormat();
format.color = 0x000000;
format.font = arial.fontName;
format.size = 20;

var field:TextField = new TextField();
field.antiAliasType = AntiAliasType.ADVANCED;
field.autoSize = TextFieldAutoSize.LEFT;
field.embedFonts = true;
field.defaultTextFormat = format;
field.selectable = false;
field.text = "GRADIENT TEXT";
addChild(field);

var bitmapdata:BitmapData = new BitmapData(field.width, field.height, true, 0);
var matrix:Matrix = new Matrix();
bitmapdata.draw(field, matrix);

var bitmap:Bitmap = new Bitmap(bitmapdata, "auto", true);
bitmap.cacheAsBitmap = true;
bitmap.x = Math.round((stage.stageWidth-bitmap.width)*.5);
bitmap.y = Math.round((stage.stageHeight-bitmap.height)*.5);
addChild(bitmap);
removeChild(field);

var gradientmatrix:Matrix = new Matrix();
gradientmatrix.createGradientBox(bitmap.width, bitmap.height, Math.PI/2);

var shape:Shape = new Shape();
shape.graphics.beginGradientFill(GradientType.LINEAR, [0x000000, 0xffffff, 0x000000], [1,1,1], [0, 130, 255], gradientmatrix);
shape.graphics.drawRect(0, 0, bitmap.width, bitmap.height);
shape.graphics.endFill();
shape.cacheAsBitmap = true;
shape.x = Math.round((stage.stageWidth-bitmap.width)*.5);
shape.y = Math.round((stage.stageHeight-bitmap.height)*.5);

shape.mask = bitmap;
addChild(shape);
Reply With Quote  
wraevn's Avatar wraevn wraevn is offline wraevn lives in United States 2009-01-16 #3 Old  
Okay - so, what I have up above also works in Flash (and you don't need to take a bitmap snapshot).

But I guess what you're saying is try this in Flex?
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2009-01-16 #4 Old  
Yeah try that out in Flex. My above was is just another way to do it you know what I mean. Also try embedding a different font than Arial. Maybe a non-standard font or something like that.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: