Java中如何获取真实的字符串高度?
我正在使用 FontMetrics.getHeight() 来获取字符串的高度,但它给了我一个错误的值,切断了字符串字符的下降部分。 我可以使用更好的功能吗?
I'm using FontMetrics.getHeight()
to get the height of the string, but it gives me a wrong value, cutting off the descenders of string characters. Is there a better function I can use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面的
getStringBounds()
方法基于当前Graphics2D
字体的GlyphVector
,该方法非常适合单行文本字符串:为了清楚起见,我省略了导入。
结果:
The
getStringBounds()
method below is based on theGlyphVector
for the currentGraphics2D
font, which works very well for one line string of text:Note that I've omitted the imports for clarity.
The result:
是什么让您认为它返回了错误的值? 您对返回结果的期望与规范不符的可能性要大得多。 请注意,如果字体中的某些字形超过或低于这些值,那就完全没问题。
getMaxDescent()
和getMaxAscent()
应该告诉您字体中任何字形的这些字段的绝对最大值。如果您想了解特定字符串的指标,那么您肯定需要调用 getLineMetrics()。
What makes you think it returns the wrong value? It's far more probable that your expectation of what it returns does not match the specification. Note that it's perfectly fine if some glyphs in the Font go over or under those values.
getMaxDescent()
andgetMaxAscent()
should tell you the absolute maximum values of those fields for any glyph in the Font.If you want to know the metrics for a specific String, then you definitely want to call
getLineMetrics()
.FontMetrics.getAscent()和 FontMetrics.getDescent() 可能会成功。
FontMetrics.getAscent() and FontMetrics.getDescent() might do the trick.
getHeight()
无法截断字符串的下降部分,只有绘制字符串才能做到这一点。 您正在使用从getHeight
返回的高度以某种方式绘制字符串,并且可能您误用了高度。 例如,如果将字符串的起点定位在 getHeight() 高的框的底部,则文本的基线将位于框的底部边缘,并且下降部分很可能会被剪裁。文本几何是一个复杂的话题,充满了奇异的历史文物。 正如其他人所建议的,使用
getAscent
和getDescent
尝试将基线正确定位在您的框中。getHeight()
can't cut off the descenders of a string, only drawing the string can do that. You are using the height returned fromgetHeight
to draw the string somehow, and likely you are misusing the height. For example, if you position the start point of the string at the bottom of a box that is getHeight() high, then the baseline of your text will sit on the bottom edge of the box, and very likely the descenders will be clipped.Text geometry is a complex topic, infused with bizarre historical artifacts. As others have suggested, use
getAscent
andgetDescent
to try to position the baseline properly within your box.我最近编写了下面的代码,因为我需要对字体的特定范围(例如:所有较低的字符或所有数字)进行像素完美的高度测量。
如果您需要更快的代码(我的代码有 for 循环),我建议在启动时运行一次以获取数组中的所有值(例如从 1 到 100),然后使用该数组。
该代码基本上将输入字符串中的所有字符绘制在 250x250 位图上重叠的同一位置(如果需要则增加或减少),它开始从顶部查找像素,然后从底部查找像素,然后返回找到的最大高度。 即使它是为字符范围设计的,它也适用于普通字符串。 这意味着在评估常规字符串时存在某种冗余,因为某些字符重复。 因此,如果您的输入字符串超过字母表计数 (26),请使用“tRange”输入:“abcd...z”和其他可能使用的字符。 它更快。
希望有帮助。
I recently wrote the code below as I needed pixel perfect height measurements for specific ranges of the font (for example: all lower characters, or all numbers).
If you need faster code (mine has for loops) I would recommend running it once at the start-up to get all values (for example from 1 to 100) in an array and then use the array instead.
The code basically draws all characters from the input string at the same place overlapped on a 250x250 bitmap (increase or reduce if needed), it starts looking for pixels from top, then from bottom, then it returns the maximum height found. It works with normal strings even if it was designed for character ranges. This means there is a sort of redundancy when evaluating regular strings as some of the characters repeat. So if your imput string exceeds the alphabet count (26), use as 'tRange' imput: "abcd...z" and other characters that may be used. It is faster.
Hope that helps.