Codename One - Method to enlarge or reduce all the fonts in all the styles

大兔子大兔子 提交于 2019-12-08 06:33:39

问题


Suppose that I have a Codename One app with a complex set of themes (that are overlayed) or an app that has only one theme that is complex.

My question is if I can have simple Java code to enlarge or reduce by a given percentage all the font sizes (that are in mm) of all styles.

I know that I can overlay a theme that changes all the font sizes, but in some conditions can be painful.


回答1:


int fontSize = Display.getInstance().convertToPixels(3);
Font tt  = Font.createTrueTypeFont(s, s).derive(fontSize, Font.STYLE_PLAIN);

For more ,please refer this link

https://www.codenameone.com/javadoc/com/codename1/ui/Font.html




回答2:


Thank you Shai and thank you tizbn, I meditated on both your answers. After that, I created the following method to do what I asked, maybe can be useful to other people.

/**
 * Increases or reduces of a given percentage all the font sizes of a given
 * theme, overlaying a new theme containing only the new font sizes. The
 * font sizes will be increased when the percentage is positive, they will
 * be reduced when the percentage is negative. Legal percentage values are
 * from -90 to 300.
 *
 * Example of usage: changeFontSizesOfTheme(theme, "Theme", 50);
 *
 * @param theme the given resource file
 * @param themeName the name of given theme
 * @param percentage the given percentage to increase or reduce the font
 * sizes
 */
public static void changeFontSizesOfTheme(Resources theme, String themeName, int percentage) {
    if (theme.isTheme(themeName) && percentage != 0 && percentage >= -90 && percentage <= 300) {
        Hashtable hashtable = theme.getTheme("Theme");
        Hashtable overlay = new Hashtable();
        Set<String> keys = hashtable.keySet();
        Iterator<String> itr = keys.iterator();
        String key;
        Object value;
        int count = 0;
        while (itr.hasNext()) {
            key = itr.next();
            value = hashtable.get(key);
            if (value instanceof Font) {
                Font originalFont = (Font) value;
                float originalFontSize = originalFont.getPixelSize();
                float newFontSize = originalFontSize * (100 + percentage) / 100;
                overlay.put(key, originalFont.derive(newFontSize, originalFont.getStyle()));
                count++;
            }
        }
        UIManager.getInstance().addThemeProps(overlay);
        Log.p(count + " font sizes of the theme \"" + themeName + "\" were overlayed");
    }
}


来源:https://stackoverflow.com/questions/48797731/codename-one-method-to-enlarge-or-reduce-all-the-fonts-in-all-the-styles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!