java - 如何获取字体的行高,例如在 JTextArea 中?

标签 java swing fonts jtextarea fontmetrics

我需要获取从一行的基线到下一个文本行的基线的距离,就像它在 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/

相关文章:

java - 发送重力消息时出现错误#2173

java - 使用Retrofit 2.0时如何将Json结果转换为Object

opengl - 使用GLUT位图字体

google-chrome - 在 ubuntu 16.04 中,书签和标签的字体大小对于 chrome 来说变小了

java - java中反序列化对象

java - 在java webapp中使用现有的sqlite数据库

java - 禁用 JXDatePicker/JXMonthView 中的日期

java - JTabbedPane 中的 XML 解析和显示

java - 如何组织我的代码以便能够使用两种不同的 View 类型?

iOS,在 UIImage 上绘制带笔划的文本