Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set text on a flex Graphics object?

Is there anyway I can effectively call the equiv. of setText on a Flex Graphics object ?

Specifically I'm using a custom cell renderer to display data in a datagrid. The renderer uses the Graphics object to set the background colour of only a portion of the cell (say the left half) based upon the underlying data. I would like to be able to have text over only that portion of the cell which has the background colour (eg centred within the left half).

I know all the widths (of both the cell itself and the graphics object) but I don't know how I can set text (centred) over the graphics object.

Anyone got any ideas pls? Tks vm

like image 899
AlexC Avatar asked Nov 26 '25 00:11

AlexC


1 Answers

Here's a solution that avoids adding UI components:

    private function WriteText(text:String, rect:Rectangle):void
    {
        var uit:UITextField = new UITextField();
        uit.text = text;
        uit.width = rect.width;
        var textBitmapData:BitmapData = ImageSnapshot.captureBitmapData(uit);
        var matrix:Matrix = new Matrix();
        var x:int = rect.x;
        var y:int = rect.y;
        matrix.tx = x;
        matrix.ty = y;
        graphics.beginBitmapFill(textBitmapData,matrix,false);
        graphics.drawRect(x,y,uit.measuredWidth,uit.measuredHeight);
        graphics.endFill();
    }

The idea is to use the UITextField to create a bitmap, that can be used with the graphics object to fill a rectangle.

This is based on a blog post by Olga Korokhina at Adobe Developer Connection.

like image 161
David Pond Avatar answered Nov 28 '25 04:11

David Pond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!