Codename One - Modify a font size in a theme like in an hashtable

丶灬走出姿态 提交于 2019-12-30 11:34:07

问题


In the previous question Codename One - Method to enlarge or reduce all the fonts in all the styles I replied with a method that iterates all the values of a theme and, if a given value is an istance of Font, it changes its font size of a given percentage.

The problem is that it doesn't work as expected, because some fonts are changed and others are not. For example, if I have the following font size of a Label (3.1mm) in my theme.xml, that Label font size it's not changed:

<font key="Label.font" type="ttf" face="0" style="0" size="0" name="native:MainThin" family="native:MainThin" sizeSettings="3" actualSize="3.1" />

What's wrong in the following code?

/**
 * 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");
    }
}

回答1:


The error was the line:

    Hashtable hashtable = theme.getTheme("Theme");

The correct code is without the double quotes:

    Hashtable hashtable = theme.getTheme(Theme);

That's all :)



来源:https://stackoverflow.com/questions/48851909/codename-one-modify-a-font-size-in-a-theme-like-in-an-hashtable

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