SWT - OS agnostic way to get monospaced font

前端 未结 5 1849
陌清茗
陌清茗 2020-12-01 10:56

Is there a way in SWT to get a monospaced font simply, that works across various operating systems?

For example. this works on Linux, but not Windows:



        
5条回答
  •  盖世英雄少女心
    2020-12-01 11:28

    For people who have the same problem, you can download any font ttf file, put it in the resource folder (in my case /font/**.ttf) and add this methode to your application. It's work 100 %.

    public Font loadDigitalFont(int policeSize) {
        URL fontFile = YouClassName.class
                .getResource("/fonts/DS-DIGI.TTF");
        boolean isLoaded = Display.getCurrent().loadFont(fontFile.getPath());
        if (isLoaded) {
            FontData[] fd = Display.getCurrent().getFontList(null, true);
            FontData fontdata = null;
            for (int i = 0; i < fd.length; i++) {
                if (fd[i].getName().equals("DS-Digital")) {
                    fontdata = fd[i];
                    break;
                }}
            if (fontdata != null) {
                fontdata.setHeight(policeSize);
                fontdata.setStyle(SWT.BOLD);return new Font(getDisplay(), fontdata));}
        }return null;   }
    

提交回复
热议问题