I'm instantiating a spark label in an ActionScript class, then setting various styles on it (such as fontFamily and fontSize):
var versionLabel:Label = new Label(); versionLabel.text = "10.1.1 SP1"; versionLabel.setStyle("fontFamily", '_verdana'); versionLabel.setStyle("fontSize", 26);
After doing all this, I'm tracing the width of the text in the label:
trace("The 'measured' width of versionLabel is:" + versionLabel.measureText(versionLabel.text).width);
However, this always gives me the width of the text as if I had not set any styles. In the case above, the on-screen width is 290 pixels with the formatting and 127 without, but the trace always returns 127.
What am I doing wrong here? Does this have something to do with the fact that I'm using an embedded font? How can I get the width of the formatted text?
So I know this is real late, but hopefully if someone else has this problem and runs accross this they might find my solution helpful. This one way I used to solve this issue. I created the following function:
private function getTextWidth(target:Label):Number
var txtField:TextField = new TextField();
var txtFormat:TextFormat = new TextFormat();
txtFormat.font = target.getStyle("fontFamily");
txtFormat.size = target.getStyle("fontSize");
txtField.defaultTextFormat = txtFormat;
txtField.multiline = true;
txtField.wordWrap = true;
txtField.width = target.width;
txtField.text = label.text;
This looks like a good solution on paper, I'll have to try it out.
I managed to hack a temp solution for extending number of datagrid table rows
based on content in the data cell, but it was very kludgey and only worked for my specific font.
Will this return textHeight as well?
Europe, Middle East and Africa