Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

textfield with padding

Contributor ,
May 16, 2013 May 16, 2013

I want to create a textfield with a coloured background BUT which some space around the text inside it, so the text doesn't start against the edges of it's background color. I can only textformat the left and right margin, so I found this:

var myFormat:TextFormat = new TextFormat;

myFormat.leftMargin = 40;

myFormat.rightMargin = 40;

var myText:TextField = new TextField;

myText.defaultTextFormat = myFormat;

myText.autoSize = TextFieldAutoSize.CENTER;

myText.y=40

var mySpr:Sprite = new Sprite();

mySpr.graphics.beginFill(0xeeaaaa,1);

mySpr.graphics.drawRect(0,0, myText.width,myText.height+80);

mySpr.graphics.endFill();

addChild(mySpr);

mySpr.addChild(myText);

This uses a coloured Sprite and inside it a textfield which has a space around of of 40 pixels. This textfield is still empty. That's because I've got this complete movieclip inside the Library. I add it through:

var clippy:textfieldpadding = new textfieldpadding()

addChild (clippy)

And what I want to do is place it like this, but than add some text to it.

var clippy:textfieldpadding = new textfieldpadding()

addChild (clippy)

clippy.myText.text = "fake text"

This doesn't work (error #1010). What does?

TOPICS
ActionScript
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

Did you name the textfield inside the clippy sprite "myText" ( not "mytext", not "MyText", not "Mytext")?

If yes, enable debugging to get the exact line where the error occurs

(btw: It s a useful convention to name classes with a leading Capital)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013

The textfield is actionscript generated. So the library item doesn't have any object on stage, just the actionscript.

I placed that library movieclip on stage. So now the coloured sprite is visible with an empty textfield on top. Then I want to target that empty - actionscript generated - textfield which I named myText as you can see from the first code, and add text toe it. But now I get this error with the clippy.myText.text = 'fake text' line: TypeError: Error #1010: A term is undefined and has no properties.  I've also tried clippy.mySpr.myText.text, but that also gave an error.

How can I target a textfield?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

So the library item doesn't have any object on stage, just the actionscript.

You can`t reference an object that is not even created in a class.

You have to wait for it to be created.

This can be done by adding a Event.ADDED_TO_STAGE listener to the textfield, and only calling

clippy.myText.text = "fake text" after that event fires

addChild() alone is not enough to make sure the object is available

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013

That doesn't work either. I've tried it with this clip in the library:

var myFormat:TextFormat = new TextFormat;

myFormat.leftMargin = 40;

myFormat.rightMargin = 40;

var myText:TextField = new TextField;

myText.defaultTextFormat = myFormat;

myText.autoSize = TextFieldAutoSize.CENTER;

myText.y=40

var mySpr:Sprite = new Sprite();

mySpr.graphics.beginFill(0xeeaaaa,1);

mySpr.graphics.drawRect(0,0, myText.width,myText.height+80);

mySpr.graphics.endFill();

addChild(mySpr);

myText.name="dynText"

mySpr.name="dynSpr"

mySpr.addChild(myText);

And then placing it through this:

var Clippy:textfieldpadding = new textfieldpadding();

Clippy.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

function onAddedToStage(event:Event):void

{

          Clippy.dynSpr.dynText.text = "fake text";

          //Clippy.mySpr.myText.text = "fake text";

          //Clippy.dynText.text = "fake text";

          //Clippy.myText.text = "fake text";

}

addChild(Clippy);

Also the commented lines don't work

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

The devil is in the details:

This can be done by adding a Event.ADDED_TO_STAGE listener to the textfield

you have to add the ventlistener to "myText" not to the MovieClip that is supposed to to hold your dynamically generated textfield

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013

Ok, but that wouldn't work either. As I'm trying to set the .text property of myText from outside that library item. So from the main timeline I add the 'textfieldpadding' movieclip from the library and then I want to use .text to access that textfield which is inside that library movieclip.

I could use that myText.addEventListener function, but then I can only use .text on it from within that Library object's code. And I can't use myText.addEventListener in the main timeline's code as there it doesn't yet now what or where myText is.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

you have contradictory messages in this post:

at one point you insist:

The textfield is actionscript generated

then in the last post you say:

I want to use .text to access that textfield which is inside that library movieclip.

If your textfield is already in the movieclip (you put it manually there) then it is not "actionscript generated" (the parent might be).

In this case it is as easy easy as:

var _mc:MC = new MC();

_mc.mytext.text = "HELLO WORLD";

addChild(_mc);

for this to work you have to meet these demands:

1.Actionscript Linkage for the MovieClip is turned on and set to MC

2.The Textfield inside the MovieClip has an instance name of "mytext" and is set to type dynamic (YProperties Panel)

3.The Font has to be embeded

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013

Maybe I wasn't clear about what I meant. Let me explain what I've done.

I start a 'New Symbol' call it textfieldpadding, turn 'export for actionscript' on and go directly to 'Actions' where I type this:

var myText:TextField = new TextField;

addChild(myText);

myText.text = "old text"

Then I go back to the main timeline. There in 'Actions' I type:

var Clippy:textfieldpadding = new textfieldpadding();

addChild(Clippy);

Clippy.myText.text="new text"

But that last line doesn't work. I can't target any objects inside that Clippy movieclip.

With 'I want to use.text to access that textfield which is inside that library movieclip' I mean the textfield which INSIDE that library movieclip is generated dynamically.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

Now I see. I don`t even want to ask, why you attempt to scatter your code like this.

Let`s assume its a simple braingame.

If you really want to create such nasty nestings, you will have to create your own CustomEvent that will bubble up from the TextField to the root.

On the main timeline you will have sth like this:

var Clippy:Textfieldpadding = new Textfieldpadding();

addChild(Clippy);

Clippy.addEventListener(CustomEvent.EVENT_DEFAULT, cutomEventHandler);

function customEventHandler(e:Event):void{

     e.currentTarget.myText.text = "new text";

}

and inside textfieldpadding you write this:

this.addEventListener(Event.ADDED_TO_STAGE,addedHandler);

function addedHandler(e:Event):void{

    var myText:TextField = new TextField();

    myText.name = "myText";

    this.addChild(myText);

    this.myText.text = "old text";

    dispatchEvent(new CustomEvent(CustomEvent.EVENT_DEFAULT));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013

Why I want to do it like this?

From the root I want to place a variable amount of such empty textfield symbols on the main stage. Through a 'for' loop in which during each run the textfield is filled with different random text.

So every time, the amount of textfield symbols and each one's text varies.

I could do this of course by placing an empty textfield in a symbol with a linkage name etc. and than in the root target it that way and add the text. But was wondering if it also could be done with textfield which are coded inside symbols. Another problem with actual textfields inside that symbol is that when it is filled from the root with very long text, the sprite in the background of that symbol should grow in height according to the textfield's height too. Which it now wouldn't

But turns out you can't target any dynamic objects inside a symbol, not just textfields, but also not sprites, movieclips etc.?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

It´s simply a logisitc nightmare you are creating for yourself. by mixing timeline code the way you attempt it.

. Another problem with actual textfields inside that symbol is that when it is filled from the root with very long text, the sprite in the background of that symbol should grow in height according to the textfield's height too.

you can always write a simple function that will adapt your width/height according to you stringlength, so that is not really an argument.

Anyway...whatever works for you, good luck

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013

Having no luck yet

This time I've tried it with a library symbol which has an actual textfield on its stage. In actionscript I'm drawing a 'fill' behind it so no matter how large the textfield becomes, it always has a coloured background with some padding around the text.

Now the problem is not making the textfield to grow in size when I give it large amount of text from the root. It's set to autosize. The problem is that I can't get it's current height whenever I place it on the root stage and give it text. When I use (myClip.myText.height) it always traces the height the textfield has as default in the symbol when it's empty. The textfield in the symbol has a height of 40.

When I place it on stage and add text to it through .text, the textfield grows in height as it should, but tracing it's height then keeps on tracing it's original 40. I can't have it trace it's height when it's filled with text. Which is what I want as I want to space them out vertically with about 5 pixels in between them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
May 17, 2013 May 17, 2013

This works for me:

var tf:TextField = new TextField();

tf.text  = "This IS a LOOOOOOOOOO\nOOOOOOOOOOOO\nOOOOOOOOOONG Text";

addChild(tf);

tf.multiline = true;

var _width:Number = tf.width;

var _height:Number = tf.height;

trace(_width+"/"+_height);

//traces 100/100

setDimensions(tf,tf.length);

function setDimensions(txtField:TextField,_txtlength:int):void{

    txtField.width = _width/20*_txtlength;

    txtField.height = _height/40*_txtlength;

    trace(txtField.width);

    //now 260

    trace(txtField.height);

    //now 130

}

Again: if you absoultely want to stick to the nested dynamic creation thingy you will have to dispatch a custom Event from the textfield to the root, to measure the textfields dimensions "after" its available on stage and the text is set

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 17, 2013 May 17, 2013
LATEST

Experimented a bit and decided to use .textHeight to read the height of the textfield after it has been filled with text (.height only gave the height of the empty textfield). And space the objects out according to that instead.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines