views:

53

answers:

3

How can i change font size in all flex application?

A: 

You can create a Factory class which will take care of text formatting, so you'll have a single place where to change the font size.

public class OtherClass
{
   public function OtherClass()
    {
        var textfield:TextField = new TextField();
        textfield = Factory.formatText(textfield );
        textfield.text = "Hello World";

        addChild(textfield );
    }
}
public class Factory
{
   public static function formatText(tf:TextField ):TextField
   {
        var fontName:String = "YourFont";
        var fontSize:int = 12;
        var fontColor:uint = 0x000000;

        var format:TextFormat = new TextFormat( fontName, fontSize , fontColor );

        tf.defaultTextFormat = format;
        //etc...

        return tf;
   }
}

You can , of course , pass other parameters to the function in order to retain some flexibility in your text formatting.

PatrickS
Imports are not to be placed inside a class declaration. Also this is not really Flex but rather pure AS.. And btw. creating a new TextFormat for every single text field is unnecessary. I would just add the used TextFormats in the class as constant fields and use it whenever needed.. Much easier than with a special factory class.
poke
@poke you're right! i have a tendency to write too quickly and think after... the imports came as an after thought, so of course they shouldn't be there. as for the Factory class, I do find it handy , maybe overkill just for font size but it's here as an idea. As for Flex vs pure AS3 , it doesn't hurt to have both approaches.
PatrickS
Pretty restrictive and not good solution since we can use `fontSize` style.
Maxim Kachurovskiy
+2  A: 

Write a style like

<mx:Style>
   global {
       fontSize: 20;
   }
</mx:Style>

in your main application. It should be inherited by application contents.

alxx
Some components have explicit `fontSize` set in them - e.g. `ToolTip`.
Maxim Kachurovskiy
A: 

Here is a solution to change the font size at runtime.

Maxim Kachurovskiy
I personally like your other solution that can update all of the styles in the application as well: http://cookbooks.adobe.com/post_Change_font_size_in_the_whole_app_with_Ctrl___-16307.html (Although if you have a lot of style definitions it can be a bit slow)
Sly_cardinal