views:

86

answers:

4

Hi there,

I'm trying to set the width of a Textfield() object based on it's string content that I have set- Is there a way to dynamically set this once the string has been sent to the object?

I have:

var t1:TextField = new TextField()
t1.x = stage.stageWidth / 2;
t1.y = stage.stageHeight / 2;     
t1.text = "some string that i would want to render";      
t1.textColor = 0x000000;
t1.cacheAsBitmap = true;
addChild(t1);

Thanks for any suggestions... jml

A: 

you could try using the getCharBoundaries() method (it returns a rectangle surrounding a character at a specified index). Use that to get the rectangle from the first and last chars and set the width to the difference of those rectangles.

Pretty convoluted, there's got to be a better way, but if not this should work.

invertedSpear
oh great!no; i don't mind this at all... i think the general rule w/r/t working with flex and flash is that you have to accept the convoluted nature of it. :)
jml
hm; can you give an example? here is what i am trying: t1.x = stage.stageWidth / 2; t1.y = stage.stageHeight / 2; var st1:String ="some string that i would like to render"; t1.text = st1; t1.width = t1.getCharBoundaries(st1.length)[1]; t1.textColor = 0x000000; t1.cacheAsBitmap = true; addChild(t1);
jml
+1  A: 
t1.text = "Some text";
t1.width = t1.textWidth + 5;
t1.height = t1.textHeight + 5;

Why the + 5? Because Adobe sucks and adds an internal gutter around your stuff. Per the docs this is supposed to be 2px per side, but it's actually slightly more, so you add another +1 for good measure.

Cory Petosky
i have to admit tho- the textwidth thing works just fine and you don't have to add in another import.
jml
+3  A: 

TextField.autoSize?

edit:

You should read the documentation correctly, it's a member variable that actually needs to be set. I'll give you a quick example on how this works:

var tf:TextField = new TextField();
tf.text     = 'Some text.';
tf.autoSize = TextFieldAutoSize.LEFT;
tf.x = ( stage.stageWidth  - tf.width ) / 2;
tf.y = ( stage.stageHeight - tf.height ) / 2;

Alternatively you can also align the text field first and use TextFieldAutoSize.CENTER to keep it aligned in the center.

poke
sweet.thanks for this answer. I didn't see it in the autocomplete options.
jml
How did I miss that... it's like the 5th property in the live docs
invertedSpear
hm; actually- it doesn't look like this works... i'll post my code in another answer.
jml
Just edit your question, I'll update my post then to reply you :)
poke
thanks for help on the site etiquette and the docs. to be honest i find the adobe docs to be of the most off-putting manuals in existance... but in the future i'll try to do more research into the way in which you have to populate the vars. i had previously set a variable and got an error (thought it was a bool).
jml
The LiveDocs are actually (imo) one of the best manuals for programming languages, and it gives a lot information about a member's syntactical data; `autoSize:String [read-write]` for example says that its name is `autoSize`, and it's a `String` that can be both read and written to. And of course the text gives information for example what exact values you can set (in this case there are even constants for the 4 values).
poke
A: 

Here's the gist of what I am trying:

public class mysite extends Sprite {
   public var t1:TextField = new TextField();
}

public function maketext():void {
  t1.x = stage.stageWidth / 2;
  t1.y = stage.stageHeight / 2;
  t1.autoSize;
  t1.text = "some string that i would like to render";
  t1.textColor = 0x000000;
  //t1.cacheAsBitmap = true;
  addChild(t1);
}

//somewhere else:
maketext();
jml
You shouldn't answer yourself if you want to update your question. Just edit the question then instead and add the new information there, but keeping your updated question as an "answer" is not the preferred way. Nevertheless I have updated my answer.
poke