A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: getTextExtent of dynamic text strings not working

  1. #1
    Flash Explorer
    Join Date
    Sep 2005
    Posts
    52

    getTextExtent of dynamic text strings not working

    With the help of moagrius and Shotsy247 I've managed to create at runtime a simple dynamic text box and format it, but as I continue working on my flash page, I find myself stuck again, and it's probably on a similar issue.

    I want to dynamically size the text box (and, eventually, ideally resize a background movie clip behind it) based on the values returned by getTextExtent. Using my Flash MX 2004 bible, however, I am instructed to create code like the following:

    code:


    var popUpStyle:TextFormat = new TextFormat();
    popUpStyle.font = "Arial";
    popUpStyle.size = 20;
    popUpStyle.align = right;
    var fieldSize:Object = popUpStyle.getTextExtent(_root.tekst.text);

    _root.skemaVars = new LoadVars();
    _root.skemaVars.load("littletxt.txt", _root);
    _root.skemaVars.onLoad = function() {
    _root.tekst.text = this.tekst;
    tekst.setTextFormat(popUpStyle);
    };

    createTextField("tekst", 1, 10, 10, fieldSize.textFieldWidth, 30);



    Which, as near as I can tell, should not return anything, since the loadVars command that assigns the dynamic text box with the instance name "tekst" runs in a nested loop that won't return a number to the root level variable "fieldSize."

    The textfield created in the last line of code will not appear at all.

    So I tried this:
    code:

    var popUpStyle:TextFormat = new TextFormat();
    popUpStyle.font = "Arial";
    popUpStyle.size = 20;
    popUpStyle.align = right;


    _root.skemaVars = new LoadVars();
    _root.skemaVars.load("littletxt.txt", _root);
    _root.skemaVars.onLoad = function() {
    _root.tekst.text = this.tekst;
    tekst.setTextFormat(popUpStyle);
    var fieldSize:Object = popUpStyle.getTextExtent(_root.tekst.text);
    trace("getTextExtent:"+fieldSize.textFieldWidth+"p x.");
    };

    createTextField("tekst", 1, 10, 10, fieldSize.textFieldWidth, 30);



    Which the same, except I imoved where I put the line that assigns the fieldSize object. The trace command here will return the (patently incorrect) response of "getTextExtent: 114px.", and the text field still does not appear at all.


    As I move beyond using flash for simple buttons, it becomes obvious to me that my old QBasic knowledge is insufficient to understand what the heck I'm doing. I'm sure that this is a basic failure in my understanding in how AS parses commands, but none of my books, nor anything I can read here seem to clarify this for me.

    As always, any help that can be offered is very much appreciated.

    Pacem!

    --T

  2. #2
    Senior Member
    Join Date
    Oct 2004
    Posts
    134
    i never use getTextExtent (i just use the textWidth and textHeight properties) but i think what's missing from your code is these lines:
    Code:
    tekst.multiline = true; // if you expect more than 1 line in your textfield
    tekst.wordWrap = true; // if you want the text to automatically wrap to the next line
    tekst.autoSize = "left"; // by default a textfield will not resize to fit the content

  3. #3
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    your making the textformat object before the textfield has any text to measure - so the textFieldWidth is 0. you have to wait till the loaded data is available. put everything in order, inside the onLoad event.

  4. #4
    Flash Explorer
    Join Date
    Sep 2005
    Posts
    52
    Looking through my old posts, I discovered that somehow the "thanks" message that I thought I added after you solved my problem was never added.

    So, um..

    <ahem>

    Thanks, mattcow, and moagrius, for your help.

    Better late than never, right? :P
    Pacem!
    --T
    "Clearly I cannot choose the cup in front of you!"

  5. #5
    Senior Member
    Join Date
    Oct 2004
    Posts
    134
    yw

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center