How To Set A Custom Scalable Font Size(11:17, 07. Sep. 2011)

Sometimes it's necessary to set a custom font size for a component just like below.

Font f = c.getFont();
//apply 13 point font size
c.setFont(f.deriveFont(13f)); 

The biggest disadvantage of using a absolute font size is that the default font size will be totally ignored. A better way is to increase/decrease the font size relatively to the default font size and to respect the font scale factor setting too. As you maybe know from the article Synthetica Font Settings, Synthetica supports a scale font factor to increase readability.

In the example below we increase the font size relatively by 2 points and invoke SyntheticaLookAndFeel#scaleFontSize() to make the value aware of the UI-property 'Synthetica.font.scaleFactor'. Because the font size getter method already returns the scaled size, we have to calculate the scaled size for the relative value only.

//UIManager.put("Synthetica.font.scaleFactor", 125);
UIManager.setLookAndFeel(...);
...
Font f = c.getFont();
//increase font size by 2 points
c.setFont(f.deriveFont(f.getSize2D() + SyntheticaLookAndFeel.scaleFontSize(2f))); 

Note: Keep in mind that the default font size can vary by using different themes. The recommended way to set a custom font size is to do it relatively - avoid to use absolut values.

Related Links

Synthetica Font Settings