Search code examples
javafontmetrics

Java get font size given height


Is there a (neat) way, instead of getting the height of a particular font size, getting a font size of a particular font (SansSerif in this case) that produces a given height?

I could of course loop through font sizes or use some form of binary chop, but if possible I would like to use something a bit cleaner and less resource intensive. The best way I've found is using something like this.


Solution

  • When searching, TextLayout, shown here and here, will provide the tightest bounds for a given text.