Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to center text object on stage 1

Status
Not open for further replies.

csteinhilber

Programmer
Aug 2, 2002
1,291
US
I'm not really a Flash newbie, but for some reason this seemingly simple task has never come up before and I find myself stymied.

I have a multi-line, wrapping, dynamic text object that is "listening" to variable var1.

I load var1 with text from an external file (JSON, to be precise). This text can be anywhere from a couple of words to a couple dozen words (ie - 1 line to maybe 3 or 4 lines).

Loading the text works like a charm. But...

How do I:
a) resize the text field so it accomodates the entire string?
b) reposition the text field so that the text is always centered on the stage (both horizontally and vertically)?

and, for bonus points, I might like to actually adjust the font size of the text object so that no matter how much text I'm displaying, it's always taking up roughly the same amount of stage real estate (ie - less text = larger font size, more text = smaller font size). Would this be possible?

I figure I'll be loading the var1 (and thus the text object) with text, while the text object is off stage, and then positioning it on stage when loading is complete to avoid any flicker.

Any help would be appreciated.

Thanks in advance!
-Carl
 
Sorry... didn't include... I'm using Flash 8.0 Pro.


-Carl
 
Think you should nest the textfield in a movie clip, it'll then be easier to center the mc on stage.
As for the textfield, you should set it's width at the widest you would want it, and then use autoSize (left) on it. No need to make it's initial height very high since autoSize will take care of that...


As for changing the font size depending on the length of the text, that's another ball game!

Regards. Web Hosting - Web Design
03/13/05 -> OLDNEWBIE VS FLASHKIT
 
Thank you oldnewbie.

I was looking for an autosize property, but didn't find it. That's certainly part of the equation. But it, of course, only solves half the problem. It centers the text horizontally, but I'm still unclear how I would center it vertically.

I imagine it would be textobject.y = stage.height/2 - textobject.height/2. But when I do
Code:
   var1 = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla rhoncus sagittis arcu.";
   myDynText.autoSize = "center";

   trace(myDynText.height);
the autosize works great... but the trace of height returns undefined. So I'm not sure how to center vertically.

Also, I'm unclear why centering the text field nested in a MC would be any easier than centering the text field directly. It seems like I'd still need to resize the MC based on the size of the text field, wouldn't I? So somehow I need to determine the width/height of the text field. At which point the MC becomes superfluous, since I can center the text field directly.

But maybe I'm not understanding.


-Carl
 
DOH!!

I must be tired.
I should be able to take it from there, yes.

Thanks oldnewbie.

Unless you're dead set on my nesting the text object in a movieclip, I think I'll skip that. I generally try not to use movieclips as containers for simple objects unless there's a compelling reason to do so. There's a notion in my brain (however unfounded) that it adds unnecessary overhead to the swf.


-Carl
 
Ÿou're welcome. [bigcheeks]

If you ever wanted to fade that text, you'd then need to nest it in a movie clip...

The mc just holds the textfield and will automatically take on the dimensions of the expanding textfield, and is only 4k.

But it's your decision.

Regards. Web Hosting - Web Design
03/13/05 -> OLDNEWBIE VS FLASHKIT
 
Well... of course you were right, oldnewbie. I want to fade the text now ;)

But nesting the text object inside a movie clip is introducing all sorts of new problems.

I created a movieClip ("mc1") to hold the dynamic text object ("tf1"). This MC both permits the alpha tweening.

I created a second movieClip ("mc2") to hold the first. This MC actually performs the fade in and fade out independent of the _level0 timeline.


But now when I set the autosize of the tf1, contrary to your statement, one or both of the movie clips do not resize. Leaving me with half the text cropped off. I'm also finding it difficult to position the text at this point. Even if I try to use localToGlobal/globalToLocal, I can't seem to get it to locate to the center of the stage.

Any ideas? This seems like it should be so trivial. I mean, all I want to do is load in a phrase, fade it in at the center of the screen, wait a determined amount of time, fade it out, load up the next phrase and repeat.



-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top