How does Java calculate font line spacing? (Calculate spacing between 2 lines in Times New Roman)

StackOverflow https://stackoverflow.com/questions/19845263

  •  29-07-2022
  •  | 
  •  

Question

So I'm creating an extremely variable interface where even the font almost has to be variable (just trust me, I need it so it works on every resolution) But obviously, there're multiple lines if text, where there is space between the separate lines.

After some trying out I found that the space between a font isn't static (for example, 2 pixels), so it has to be a percentage of the full font (for example, percentage is 25% and the font size is 40 pixels, than the spacing is 10 pixels) or some sort of formula.

After some search work I couldn't find anything about the spacing of a font, so I thought it was Java specific. I doubt this, though. Since Java doesn't have a method where you can change this spacing (as far as I could find).

So my question is, is there any way to either change or get to know the spacing between 2 lines of Times New Roman text?

The biggest problem is that I need to know this to CREATE the font... In case needed the formula: fontSize = (myTextArea.getHeight()-([fontAscend]+[fontDescent])*21)/22

P.S. I'm using a JTextArea and prefer not to use a JEditorPane

Was it helpful?

Solution

Perhaps this is what you're after?

myTextArea.getFontMetrics().getHeight()

getFontMetrics is a method on JComponent which gives you a FontMetrics object that has a getHeight() method.

OTHER TIPS

The spacing between lines depends on whether the line of text has descenders or not.

Read the introduction to the FontMetrics class for everything you never wanted to know about ascent and descent.

It seems that with my specific problem (where I have to create a font and I need that same font to be able to create it) the only options are to either use a JEditorPane and use CSS to set the spacing to a specific value which I can use in my formula or to use algebra to get the right value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top