What's the difference between GC and FontData

2019-06-17 14:26发布

问题:

This explains font metrics

I think we can get "Font height" in SWT like these;

GC gc = new GC(label);
System.out.println( gc.textExtent(label.getText()) );
System.out.println( label.getFont().getFontData()[0].getHeight() );

Why this two outs aren't same? And which one is correct height for a string ?

回答1:

GC#textExtent() returns extent in pixels, while FontData returns in font points. The units are different here.



回答2:

Use FontMetrics.getHeight() to get height in pixels.

FontMetrics fontMetrics = gc.getFontMetrics()