Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Austin Henggi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out FontMetrics#stringWidth(String str)
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic