The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


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