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

Dynamically size movieclip to fit text's string height and width

New Here ,
May 12, 2014 May 12, 2014

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!

TOPICS
ActionScript
1.8K
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

correct answers 1 Correct answer

Engaged , May 12, 2014 May 12, 2014

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
...
Translate
Engaged ,
May 12, 2014 May 12, 2014

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;
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
New Here ,
May 12, 2014 May 12, 2014

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.

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
Guide ,
May 12, 2014 May 12, 2014

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.

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
Engaged ,
May 12, 2014 May 12, 2014

^ 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.

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
New Here ,
May 12, 2014 May 12, 2014
LATEST

thank you both! it works!

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