javafontsgraphics2dfontmetrics

Java: Getting a font with a specific height in pixels


It’s easy to determine the rendered height of a font using FontMetrics, but what about the other way around? How can I obtain a font that will fit into a specific height in pixels?

"Give me Verdana in a size that is 30 pixels high from ascender to descender."

How do I ask Java for this?


Solution

  • I don't think there's a "direct" way to find a font by height; only an indirect way... by looping through the sizes, and testing the height of each is <= required height.

    If you're doing this once, just loop through them... if you've doing it "on the fly" then do a binary search, it'll be quicker.