Copy link to clipboard
Copied
Hello,
I am trying to dynamically size a movieclip to fit the size of a text's string height and width (This text is inside the movieclip)
here is my code so far..
var Font1_ = new Font1();
var Format2:TextFormat = new TextFormat();
Format2.size = 36;
Format2.align = TextFormatAlign.CENTER;
Format2.font = Font1_.fontName;
var MessageBox:MovieClip = new MessageBoxMC();
MessageBox.Text1.defaultTextFormat = Format2;
MessageBox.Text1.embedFonts = true;
MessageBox.Text1.antiAliasType = AntiAliasType.ADVANCED;
MessageBox.Text1.wordWrap = true;
MessageBox.Text1.width = 800;
MessageBox.Text1.height = 400;
MessageBox.Text1.textColor = 0xFFFFFF;
MessageBox.Text1.cacheAsBitmap = true;
MessageBox.Text1.mouseEnabled = false;
MessageBox.Text1.text = String("Use the Arrow Buttons to move");
MessageBox.width = MessageBox.Text1.width;
MessageBox.height = MessageBox.Text1.height;
MessageBox.x = 400;
MessageBox.y = 200;
addChild(MessageBox);
this isn't working for me.. anyone know the best way to do this?
I also want the text to be centered in the movieclip, with a border of around 2-4 pixels
I am also not sure if i should be using text.length? any advice and input is greatly welcomed.
thanks in advance!
Essentially, you need to check for a change in .textWidth or .textHeight, which are different from .width and .height.
This code works for me (assumes a background box called bg and a foreground text instance fg inside a moveclip and an X and Y bounds offset variable to say how far away you want the box from the text and a textFrameNudge to provide a tweak value).
// Resize background box thisBox.bg.x = thisBox.fg.x - boundsOffsetX + textFrameNudge; // I'm using .4 as the tweak value for...
Copy link to clipboard
Copied
Essentially, you need to check for a change in .textWidth or .textHeight, which are different from .width and .height.
This code works for me (assumes a background box called bg and a foreground text instance fg inside a moveclip and an X and Y bounds offset variable to say how far away you want the box from the text and a textFrameNudge to provide a tweak value).
// Resize background box thisBox.bg.x = thisBox.fg.x - boundsOffsetX + textFrameNudge; // I'm using .4 as the tweak value for textFrameNudge thisBox.bg.y = thisBox.fg.y - boundsOffsetY; thisBox.bg.width = thisBox.fg.textWidth + boundsOffsetX*2 +textFrameNudge*thisBox.fg.getTextFormat().size; thisBox.bg.height = thisBox.fg.textHeight + boundsOffsetY*2;
Copy link to clipboard
Copied
thank you the .textWidth and .textHeight is exactly what I was looking for.
However, the position of the text is not centered in the middle of the box...
where in your code do u set format of the text to center it? and how can i use this to center the text inside the movielclip?
thanks.
Copy link to clipboard
Copied
If you assume that the registration point is the top left of the object AND that the parent is at its own zero point and also registered to the top left, to center anything, you need the center of the parent (parent.width/2, parent.height/2) - half the width and height of the child.
So:
child.x = parent.width/2 - child.width/2;
child.y = parent.height/2 -child.height/2;
If the parent's contents are not logically related to its zero point, you're pretty much screwed. If it is registered somewhere other than the top left or the child is, adjust based on your own situation.
Copy link to clipboard
Copied
^ What Amy said.
I was working with pre-built movieclips that are already on the stage, so the calculation are assuming upperleft as the alignment point.
Copy link to clipboard
Copied
thank you both! it works!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now