I'm using the 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?
views:
1452answers:
3FontMetrics.getAscent() and FontMetrics.getDescent() might do the trick.
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()
and getMaxAscent()
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()
.
getHeight()
can't cut off the descenders of a string, only drawing the string can do that. You are using the height returned from getHeight
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
and getDescent
to try to position the baseline properly within your box.