Skip navigation
Currently Being Moderated

How to create global CSS for all TextFields

Feb 11, 2013 7:34 AM

Tags: #cs5.5 #css

In most environments that support CSS, there's a way to say "You know all these things of type X? Use this stylesheet on it." (For example, HTML, FlexSDK).

 

In Flash Pro, I'm looking for a way to set a stylesheet for all TextFields. I'm sure it's really simple, but all the examples I have been able to find require a direct handle to the TextField. I thought about using the StyleManager, but it seems to only be geared to setting styles on Flash components.  I'd rather not watch the stage for ADDED_TO_STAGE, since that's a relatively expensive solution to a problem I am sure has a cheaper solution. I find it impossible to believe that the Flash team would have gone to the trouble of adding CSS support but then wouldn't have provided a way to apply that CSS without directly referencing each and every text field.

 

Can someone point me in the right direction?

 
Replies
  • kglad
    72,331 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 11, 2013 5:57 PM   in reply to Amy Blankenship

    you can find (and apply a stylesheet) to all textfields existing at any time(s) you want to check but you can't reference textfields that don't exist (yet) and there's no simple way to detect when a textfield is created or added to the display list (unless you explicitly code for that).

     
    |
    Mark as:
  • kglad
    72,331 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 11, 2013 8:48 PM   in reply to Amy Blankenship

    you could create a class that extends the textfield, apply your css to your class and use those class instances instead of the flash textfield.

     
    |
    Mark as:
  • kglad
    72,331 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 12, 2013 6:38 AM   in reply to Amy Blankenship

    you don't need the textfield handles to find them all. the function below would apply the stylesheet ss to all textfields that are on a timeline at the time the function executes.

     

    applyTextFieldStyleF(MovieClip(root),ss);

     

    function applyTextFieldStyleF(mc:MovieClip,ss:StyleSheet):void {

    for (var i:int=0; i<mc.numChildren; i++) {

    if (mc.getChildAt(i) is TextField) {

    TextField(mc.getChildAt(i)).styleSheet=ss;

    } else if (mc.getChildAt(i) is MovieClip) {

    applyTextFieldStyleF(MovieClip(mc.getChildAt(i)),ss);

    }

    }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 12, 2013 10:14 AM   in reply to Amy Blankenship

    Your method won`t work for TextFields that are already on stage. I was under the impression you would have a bunch of textfields that were distributed across the timeline.

    I use the timeline/stage (if I were adding everything through code, obviously I'd already have a handle to every TextField

    Now you are forced to add everything through code. Also you have problems with TextFields that are nested inside MovieClips. If you add the MovieClip, the textField won`t throw an added_to_stage event and won`t get styled even if it has the approbriate nomenclature. Also you can`t change the font during runtime without removing and adding the TextField.

     

    Or Am I missing something here?

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points