javajsonlibgdxbitmap-fonts

Using multiple fonts in one Json skin


I am using one skin for my menu, and I am wondering if it is possible to add several fonts to this skin and then pick which font to use programatically.

Currently I set a font and add it to the LabelStyle like this:

"com.badlogic.gdx.graphics.g2d.BitmapFont": {
        "fontTest": { "file": "outline_test.fnt" }
    },

    "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
        "default": {
            "font": "fontTest", "fontColor": "white"
        }
    }, 

Which mean that when I create a Label all I need to do to get this font is:

Label rating = new Label("Rating: ", skin);

This picks the LabelStyle that is set as default, is there any way to reference the LabelStyles set in my Json-file so that I can pick which style to use in my Java-code? Or do I need to create a seperate skin?


Solution

  • Yes you can add several font styles in Skin file

    Let' say you want to create a Label Style called "myStyleFontTest" and it uses a different font

    "com.badlogic.gdx.graphics.g2d.BitmapFont": {
            "fontTest": { "file": "outline_test.fnt" },
            "myStyleFontTest": { "file": "myStyle.fnt" }
        },
    
        "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
            "default": {
                "font": "fontTest", "fontColor": "white"
            },
            "myLabelStyle" :  {
                "font": "myStyleFontTest", "fontColor": "red"
            }
        }, 
    

    Now when you create a new label, all you have to do is

    Label testLabel = new Label(text, skin, "myLabelStyle")