Search code examples
haxe

How do I change font sizes in Heaps?


I'm currently coding in Haxe with Heaps and was checking on how to display text. One thing I want to do is give different font sizes to different texts. Naturally, I declared two Font instances and resized one of them. However, both texts are resized and I cannot manage to have them resize independently. How should I resize font sizes in Heaps?

class Main extends hxd.App{
    override function init(){
       var font : h2d.Font = hxd.res.DefaultFont.get();
       var font2 : h2d.Font = hxd.res.DefaultFont.get();
       font2.resizeTo(23);

       var tf = new h2d.Text(font);
       var tf2 = new h2d.Text(font2);

       tf.text = "Hello World\nHeaps is great!";
       tf.textColor = 0x00FF00;
       tf.x = 100;
       tf.y = 100;
       tf.textAlign = Center;

       tf2.text = "Hello World\nHeaps is great!";
       tf2.textColor = 0x00FF00;
       tf2.x = 300;
       tf2.y = 300;
       tf2.textAlign = Center;


       s2d.addChild(tf);
       s2d.addChild(tf2);

    static function main(){
        new Main();
    }
}

Solution

  • The approach taken does not work because DefaultFont.get() caches the result.

    You could either:

    • Copy the second font by doing var font2 : h2d.Font = font.clone() so that it gets its own properties.
    • Adjust scaleX and scaleY of Text.