With reference to some element, TextMetrics is able to calculate the width of some text. Is it possible to do the reverse? ie. with width provided, how many characters on a text can accomodate on that given width?
With reference to some element, TextMetrics is able to calculate the width of some text. Is it possible to do the reverse? ie. with width provided, how many characters on a text can accomodate on that given width?
I think you could do it using TextMetrics. Given the text you could keep removing characters off the end until it fits into the given width. Not the most efficient algorithm but it should be pretty easy to implement.
To amplify @skirtle's point, I think you HAVE to do it with TextMetrics. Trying to figure out how many characters will deterministically fit in a specified width (your initial request) will only work with a fixed-pitch font (Courier, monospace, etc.) If it's not a fixed-pitch font, then giving something a width to calculate text widths is useless, because it can't know the distribution of character widths you intend to render.
Therefore, the best you can do is loop to get the string width and truncate it until it fits.
stevil