Setting AS 2.0 component styles the lazy way…

Posted on Monday 5 June 2006


While producing component based applications it became very tiring to have to repeat the same process to apply styles to individual or groups of components while not resorting to using _global.styles.componentClassName and 'tarring with the same brush' so to speak. If you do this it can be a real pain to apply a clean variant to the mix. Plus I loathe using _global for anything if I can avoid it. So I decided to stop using it altogether and find a quicker (lazier) way of dealing with it :P.

I knocked together a quick utility method, that I use primarily within my component development framework. You could use it straight on the stage so to speak or add it into a class. I keep meaning to add it to a 'wrangling' class I have knocking around for dealing with other issues relating to components, once I do I'll upload it for those interested.

Anyway for those who'd like to try it out here is the 'raw' method:

ACTIONSCRIPT:
  1. function setComponentStyle(obj:MovieClip, styleObj:Object)
  2. {
  3.     for(var prop in styleObj)
  4.     {
  5.         obj.setStyle(prop, styleObj[prop]);
  6.     }
  7. }

Now all you need to do is pass in a component instance and the style object containing the elements you want applied. For this I'm just going to use a Button component that calls the method itself. Below is the basic code of our 'buttonStyle'.

ACTIONSCRIPT:
  1. var _color :Number = 0xFF0000;
  2. var _fontSize :Number = 11;
  3. var _font :String = "Arial";
  4. var _weight :String = "bold";
  5. var _embedFonts :Boolean = true;
  6.  
  7. // Don't forget to embed the font otherwise you won't see anything font related
  8. var textStyle   :Object    = {fontFamily:_font, fontSize:_fontSize,
  9.                 fontWeight:_weight, color:_color, embedFonts:_embedFonts};

Now all we need to do is pass it to the component we wish to apply the style to

ACTIONSCRIPT:
  1. // btn is the instance name of a Button component
  2. btn.onPress = function()
  3. {
  4.     setComponentStyle(this, textStyle);
  5. }

If I wanted to add this to a selection of Button, or Label components I could easily do it as a block or via a loop:

ACTIONSCRIPT:
  1. // As individual calls
  2. // First the Buttons
  3. setComponentStyle(btn1, textStyle);
  4. setComponentStyle(btn2, textStyle);
  5. // Next our Labels
  6. setComponentStyle(lbl1, textStyle);
  7. setComponentStyle(lbl2, textStyle);
  8.  
  9. // Or in a loop
  10. for(var i = 0; i <componentsPresent.length; ++i)
  11. setComponentStyle(componentsPresent[i], textStyle);

Check out the simple example below.

placeholder for flash movie

License

This work is published under a CC-GNU LGPL.



2 Comments for 'Setting AS 2.0 component styles the lazy way…'

  1.  
    Goose
    June 5, 2006 | 8:38 pm
     

    So the benefit of this is that styles can be reused easily without using _global? Also, that function library you mentioned that you made sounds interesting.

  2.  
    June 5, 2006 | 9:06 pm
     

    Yes, I tend to utilize it within component classes that has other components that are styled within it. The additional benefit is it gets rid of the _global call and allows finer control over which elements are styled - even if they are of the same type. The idea of having ‘pathed’ calls is a big no-no as far as I am concerned within a component class. So I don’t have _parent or _global in my any of my classes. Hence how this came into being…

    I’m trying to tidy up the functions library but I haven’t had time to sort it so it to such a degree that I’d be happy to release it. Should be ready soon though :P



Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.

Comments are moderated so please only submit once


RSS feed for comments on this post | TrackBack URI