Sunday, August 20, 2017

Set font height as float, instead of integer

Leave a Comment

Is there a way to specify the font size as a float/double instead of integer. I am creating my font as:

Font font = new Font(shell.getDisplay(), "CormorantGaramond",14, SWT.NULL); 

where 14 is the font size in points. But the constructor only accept an integer, which I find odd when the font size is specified in points and not pixel. Is there a way to specify font size as a non integer value. (It is a ttf font, if that matter).

2 Answers

Answers 1

It seems that currently (Eclipse Platform Mars 4.5) there is no platform-independent way to set the font height using the value of the float type.

Reference: org.eclipse.swt.graphics.FontData (Eclipse Platform API Specification) - Help - Eclipse Platform.

Answers 2

There is a way you can set the font size in float. Java provide a built in function "public Font deriveFont(float size)" in ava.awt.Font class. This deriveFont creates a new instance of the Font based on the values you provide, so you will need to maintain a reference to it

And then you have to use the SWTUtils class to convert awt to swt Here it is SWTUtils.java

Download this file and add it to your project. Thats it.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment