07 April 2011

Changing font size for all styles in Flex at runtime

Often you may want to change the font sizes of all elements in your Flex Application. For example you want to give control to your users to change the font settings and sizes or you may want to do it automatically on smaller screens.

I have used below code, where I have used StyleManager to loop through every style and change the font size. Let's have a look:-

// adjust the font size on all defined CSS styles by increasing or decreasing pixel size
       public function adjustFontSize(increment:Number):void
        {
            var selectors:Array = StyleManager.selectors;
 
            for each (var selector:String in selectors)
            {
            var css:CSSStyleDeclaration = CSSStyleDeclaration(StyleManager.getStyleDeclaration(selector));
 
            // assume here that all font sizes in the application
            // are explicitly set by pixel size in the CSS
            if (css.getStyle("fontSize") != null)
            {
                var fontSize:Number = Number(css.getStyle("fontSize"));
                css.setStyle("fontSize", fontSize+increment);
            }
            }
        }

0 comments:

Post a Comment