fontscodenameonenativederived

Codename One: Choosing and Styling a Native Font


For my app I need a font that scales well with the device display size. I tried but did not like the system fonts small, medium and large. Specifying a size in mm is also unattractive because it has to be calculated for each device. I found that I like the font of a native button (unselected) on both iOS and Android devices. (On Android, native Labels, Buttons and TextFields have different font sizes; I want a uniform size.) So I decided to use the native button font for all my text components.

Say I add UIID MyTextField to the theme and specify everything (background, colors, etc.) except the font. The font I want to derive from a native Button. However, the Developer Guide says (in 3.3.13): 'always inherit only from UIID’s you defined e.g. MyButton'. So how do you access the native Button font? I decided to set the font in code and that works:

// includeNativeBool == true
Font myFont = new Button().getUnselectedStyle().getFont();
...
TextField textField = new TextField();
textField.setUIID("MyTextField");
textField.getAllStyles().setFont(myFont);

Is this the correct solution? Or is there a way to handle the font in the Style?


Solution

  • The button font size is defined in millimeters in the themes for the respective OSs. MM size is uniform based on display density so it should be the closest thing to uniform size you can get.

    What you did is a hack. It will work in theory but it looks ugly and will behave inconsistently and unpredictably as we change the underlying native theme design.