我需要获取从一行的基线到下一个文本行的基线的距离,就像它在 JTextArea 中的布局一样。为此,我只有指定字体大小的字体。
我的问题是,在字体上,我只能获取 LineMetrics 或 FontMetrics,它们都有不同大小的线条 (getAscent/getHeight/getDescent),具体取决于当前文本 - 例如,如果有字母 f
,上升幅度更大,如果有g
,我有下降。
有没有办法获得“全局”字体高度属性?所以我将能够在 Graphics2D 上正确绘制文本。
最佳答案
来自 FontMetrics.getHeight()
的 JavaDoc :
Gets the standard height of a line of text in this font. This is the distance between the baseline of adjacent lines of text. It is the sum of the leading + ascent + descent. Due to rounding this may not be the same as getAscent() + getDescent() + getLeading(). There is no guarantee that lines of text spaced at this distance are disjoint; such lines may overlap if some characters overshoot either the standard ascent or the standard descent metric.
因此,如果您的字体实现正确,这应该是您应该放置在两行文本之间的距离。
关于java - 如何获取字体的行高,例如在 JTextArea 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12777758/