File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How do I draw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I draw "Best-Fit" text inside of a rectangle?" Watch "How do I draw "Best-Fit" text inside of a rectangle?" New topic
Author

How do I draw "Best-Fit" text inside of a rectangle?

Austin Henggi
Greenhorn

Joined: Jul 08, 2005
Posts: 10
I simply want to draw text inside of a rectangle. Easy. Harder: I would like the text to be as readable (translate: big) as possible with the constraint that it must be contained in the rectangle. I know about getAscent() and getDescent() and how to iterate through font types and sizes to find something that fits. But is there a more efficient / existing code for finding best font size to render a text string in a rectangle so that it is as readable as possible?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

Check out FontMetrics#stringWidth(String str)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Or if you need greater accuracy on the bounding rectangle, look into TextLayout (and LineBreakMeasurer?)

No matter how you approach this, obtaining the font size for the "best fit" will still be an iterative process.


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I draw "Best-Fit" text inside of a rectangle?