R – set text on a flex Graphics object

apache-flexdatagridgraphics

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

Best Answer

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.

Related Topic