New version - 16 Aug 11
New method to get the required font into the Display Context for sizing via GetTextExtentPoint32W. Default code (by trancexx ) or you can add the handle of your GUI to use that font directly.
The UDF did not correctly recognise TAB characters and so undersized lines containing them. Adding 1 to the $iAttrib paramter will expand tabs to "XXXXXXXX" before sizing the text. This might mean that the returned size is slightly too wide, but at least the line will fit! See the ExtMsgBox thread for an example showing this.
One or two minor code changes (improvements I hope!). Note that that these are non-scriptbreaking changes - unless you need to expand tabs you use StringSize just as before. I realise the use of $iAttrib to pass the "expand tab" is less that ideal, but it does prevent using another parameter.
New UDF and new examples below and in zip.
3 Sep 10: Minor code change. Thanks xrewndel
19 Jun 10: Minor code change. Thanks Mat
Amended version posted 20 May 2010: - Error returns now correct
I just realised that although I have posted versions of this UDF many times in Help topics, I had never actually posted a "final" version here in Example scripts - better late than never, I suppose!
StringSize takes a text string and calculates the size of label required to hold it as well as formatting the string to fit.
Now AutoIt will, of course, size a label automatically to fit a text string, but it will not format the string in any way - what you use as the string is what you get in the label. If you do set any label sizes the text will be wrapped, but you can only determine the correct size of the label by trial and error.
StringSize will, however, reformat the string to fit in a given width and tell you the required height so that you can read it all - whatever the font type or size - and you do not have to do the formatting beforehand.
Here is a simple example to show what I mean (you need the UDF in the same folder for all the examples):
And here is an example showing how StringSize can deal with different fonts and text sizes:
You can see that the GUI is perfectly sized each time and that the button is always the right size and in the right place. StringSize returns an array which contains the formatted text to display and the size of the label needed to display it. All you need to do is to use the array elements when you create your label. Try changing the values in $aFont and $aSize if you want to try you own favourites - but beware, StringSize will return an error if you make the size so large that it cannot fit a word into the label width.
NEW A more complex example showing how formatted and unformatted text can be sized correctly. The width of GUI holding the unformatted text varies randomly in width (the current value is displayed at top right):
NEW And a final example showing how you can get your text in the largest possible font to fit in a given space:
NEW Finally here is the UDF itself:
NEW And all 5 files in zip format:StringSize.zip
I hope you find this useful - I certainly do.