Tag Archives: JAVA third-party font

JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)

Java using the third-party font library method, using the third-party TTF/TTC font library method

Sometimes we use Java fonts in our programs, but not in all font systems. We may use external custom fonts, so we will work less in program migration and deployment. Recently, we used custom font files in a project. If we straighten them out, remember them.

class Loadfont
{
    public static Font loadFont(String fontFileName, float fontSize)  //The first parameter is the external font name, the second is the font size
    {
        try
        {
            File file = new File(fontFileName);
            FileInputStream aixing = new FileInputStream(file);
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
            aixing.close();
            return dynamicFontPt;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return new java.awt.Font("宋体", Font.PLAIN, 12);
        }
    }
    public static java.awt.Font Font(){
        String root=System.getProperty("user.dir");//Project root path
        System.out.println(root);
        Font font = Loadfont.loadFont(root+"/simsun.ttc", 12f);//call
        return font;//return the font
    }
    public static java.awt.Font Font2(){
        String root=System.getProperty("user.dir");//Project root path
        System.out.println(root);
        Font font = Loadfont.loadFont(root+"/simsun.ttc", 12f);
        return font;//return the font
    }
}

usage method:

String root= System.getProperty (” user.dir “);
String fontname = “Droid Serif”;
Font font = Loadfont.loadFont (root+”/ xx.ttf “, 16F); — & gt; to the required xx.ttf Font library file.
If not, you can try to double-click the TTF file, install it (the previous question is Windows system), the above internal class, test can, do not need to install the TTF file.

Or directly put the TTF file into the JRE/lib/fonts directory. If there is no directory, you can MKDIR

In new font (“DDD”, XX, XX); – & gt; DDD is double-click aa.ttf The file font name, not the file name.