JAVA ttf字体转化为Font

阅读 93

2022-05-06

public static Font ttf2font(String filePath, int fontStyle, int fontSize) {
	File fontFile = new File(filePath);
	Font font = null;
	try {
		font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
		font = font.deriveFont(fontStyle, fontSize);
	
	} catch (FontFormatException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return font;
}

类似这样使用:

t.setFont(TextArea.ttf2font("./Consolas.ttf", Font.BOLD, 20));

精彩评论(0)

0 0 举报