Measuring the width of text without rendering a control

145 views
Skip to first unread message

Denis

unread,
Apr 23, 2008, 11:38:47 AM4/23/08
to Flex India Community
Hi all,

I need to measure the size of the number of text fields that come from
the XML to display them in a datagrid. The size of datagrid columns
depends on the width of the text records that come from the database
as the text cannot wrap so the columns should adjust if possible. The
logic is a bit complex since I need to know the width of all text in
all columns before distributing width among columns.

How can I measure the width of the text without adding a child text
control to my parent object to measure the text. I would like to avoid
creating a dummy textbox in my container just to measure the width of
the text records but I did not find anything on the web that deals
with this.

I was planning to measure the text width after the data is loaded
since it needs to be done only once. Can I create an "abstract" Text
object and measure the size of the text within it? I apologize for
awkward wording but I am not very experienced in Flex yet.

Thanks in advance,
Denis

Abdul Qabiz

unread,
Apr 24, 2008, 9:54:11 AM4/24/08
to flex_...@googlegroups.com
You can do that if you know about font. There are tools which can
export font glyph information i.e. Width, height, ascent, descent, etc
for characters.

You can do it other way, you can create a invisible textfield,
populate the text and fine out the information.

--
Sent from Gmail for mobile | mobile.google.com

-abdul
---------------------------------------
http://abdulqabiz.com/blog/
---------------------------------------

Vinoth Babu

unread,
Apr 25, 2008, 1:14:53 AM4/25/08
to Flex India Community
Hi Denis,.

Try this code

public function optimizeGridColumn (dg:DataGrid):void
{
var dgCol:DataGridColumn;
var renderer:UITextField;
var tf:TextFormat;
var col:int;

if (dg.columnCount > 0 && dg.dataProvider != null)
{
// initialize widths array
var widths:Array = new Array (dg.columnCount);
for (col = 0; col < widths.length; ++col)
{
widths[col] = -1;
}

// go through each data item in the grid, estimate
// the width of the text in pixels
for each (var item:Object in dg.dataProvider)
{
for (col = 0; col < widths.length; ++col)
{

renderer = new DataGridItemRenderer();
// Must add to datagrid as child so that it inherits
// properties essential for text width estimation,
// such as font size
dg.addChild(renderer);

dgCol = dg.columns[col] as DataGridColumn;
renderer.text = dgCol.itemToLabel(item);
widths[col] = Math.max(renderer.measuredWidth + 10,
widths[col]);

// remove renderer from datagrid when we're done
dg.removeChild(renderer);
}
}

// go through headers in the grid, estimate the width of
// the text in pixels, assuming the text is bold
for (col = 0; col < widths.length; ++col)
{
// it's ok to reuse renderer, but I chose not
// to for safety reasons. Optimize if needed.
renderer = new DataGridItemRenderer();

// Must add to datagrid as child so that it inherits
// properties essential for text width estimation,
// such as font size
dg.addChild(renderer);

dgCol = dg.columns[col] as DataGridColumn;
renderer.text = dgCol.headerText;

tf = renderer.getTextFormat();
tf.bold = true;
renderer.setTextFormat (tf);

widths[col] = Math.max(renderer.measuredWidth + 25, widths[col]);

// remove renderer from datagrid when we're done
dg.removeChild(renderer);
}

// set width of columns to determined values
for (col = 0; col < widths.length; ++col)
{
if (widths[col] != -1)
{
dg.columns[col].width = widths[col];
}
}
}
}

- Vinoth
Reply all
Reply to author
Forward
0 new messages