getTextExtent() is really unpredictable and it works properly only with flash
player ver 7 or higher. Adobe document says its been deprecated since flash
player 8, but i dont think so and their doc must have wrongly said so.
anyway, heres another approach where textWidth and textHeight properties of
textField have been utilised.
var my_str:String = "Sample text";
var fmt:TextFormat = new TextFormat();
function getTextWidthAndHeight(txt, fmt, fontsize) {
fmt.font = "Arial";
fmt.bold = true;
fmt.size = fontsize;
this.createTextField("dummyTF", this.getNextHighestDepth(), 10000, 10000, 100,
100);
dummyTF.text = txt;
dummyTF.setTextFormat(fmt);
var o = new Object();
o.Width = dummyTF.textWidth;
o.Height = dummyTF.textHeight;
removeMovieClip("dummyTF");
return o;
}
var dimensionInfo = getTextWidthAndHeight(my_str, fmt, 12);
this.createTextField("newTField", this.getNextHighestDepth(), 100, 100,
dimensionInfo.Width+4, dimensionInfo.Height+4);
newTField.border = true;
newTField.wordWrap = true;
newTField.text = my_str;
newTField.setTextFormat(fmt);
Hope this helps, and plz give the feedback.
---- Arindam
"Perdue, Blake" <[EMAIL PROTECTED]> wrote:
I need to measure the width of some text. Here's the function I'm using:
private function getTextWidth(txt,fontSize) {
var fmt=new TextFormat();
fmt.font='Univers 67 CondensedBold';
fmt.size=fontSize;
fmt.autoSize='left';
var metrics=fmt.getTextExtent(txt);
return metrics.textFieldWidth;
}
This function is not predictable - for the same text, it returns
different numbers on different OSes or flash player versions:
- works fine in IE/FF for flash player 6
- doesn't work in Mac Safari for flash player 6
- works fine in IE/FF for flash player 8 using Service Pack 1
- doesn't work for IE/FF for flash player 8 using Service Pack
2
Is there another function I can use, or another way to go about
measuring the width of some text?
Blake Perdue | 212.522.1292 | AIM: blakepCNN
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
Send instant messages to your online friends http://asia.messenger.yahoo.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com