Alex Rivera | Logout

Java: Getting a font with a specific height in pixels

Asked 2011-04-29T08:19:45.787
10

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?

Edit
Report

1 Answer

8

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.

answered 2011-04-29T08:24:54.543

Your Answer