Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 6 of 6

Thread: text align

  1. #1

    text align

    anyone know how to align text in an as3 created text field:

    var loadingText:TextField = new TextField();
    loadingText.text = 'Building Interface...';
    loadingText.textColor = 0xffffff;
    loadingText.selectable = false;
    ?? loadingText.textAlign = 'center'; ??


    needless to say that doesn't work and throws an error, i can't find for the life of me how to do this if its even possible.

    thanks.

  2. #2
    BradLee's Avatar
    223
    posts
    Formerly TheSpork
    You need to use the TextFormat class.

    Code:
    var txtFormat:TextFormat = new TextFormat();
    txtFormat.align = "center";
    
    var txtField:TextField = new TextField();
    txtField.text = "Some text";
    txtField.setTextFormat(txtFormat);

  3. #3
    well that works so far as not throwing an error, but it doesnt seem to actually center the text. Its still anchored on the left. this code:

    Code:
    var myTextFormat:TextFormat = new TextFormat();
    	myTextFormat.align = TextFormatAlign.CENTER;
    
    var myText:TextField = new TextField();
    	myText.setTextFormat(myTextFormat);
    	myText.textColor = 0x999999;
    	myText.width = 400;
    	myText.background = true;
    	myText.text = 'this is a test of allignment';
    	
    	
    myText.x = stage.stageWidth/2 - myText.width/2;
    myText.y = 150;
    addChild(myText);
    produces this - http://milkish.com/test/textAlign.swf
    the text isnt aligned to the center in the text box and for some reason the text box is about 150 pixels tall on its own.

    heres the fla - http://milkish.com/test/textAlign.fla

    any thoughts?

  4. #4
    Change your code to:

    PHP Code:
    var myTextFormat:TextFormat = new TextFormat();
        
    myTextFormat.align TextFormatAlign.CENTER;

    var 
    myText:TextField = new TextField();    
        
    myText.textColor 0x999999;
        
    myText.width 400;
        
    myText.background true;    
        
    myText.text 'this is a test of allignment';
        
    myText.setTextFormat(myTextFormat);
        
    myText.stage.stageWidth/myText.width/2;
    myText.150;
    addChild(myText); 
    Note the change of order of when you apply the textFormat. Which tends to make sense, first create and populate the text field with text, the format the text fields text.

  5. #5
    crap well i finally got it working, thanks dail! also to be noted, it seems you have to re-set the textFormat each time you change the text... ugh. also im still completely in the dark as to why theres such a tall background.

  6. #6
    BradLee's Avatar
    223
    posts
    Formerly TheSpork
    You may want to use the autoSize attribute to keep your text field the right size.

    Code:
    txtField.autoSize = TextFieldAutoSize.CENTER;
    Or, you could just manually set the height yourself. A Textfield's default dimensions are fairly big.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012