BugFix version - 27 Dec 23
Fixed: No default value set for the MaxWidth parameter - took 12 years for someone to notice it!
New UDF and new examples below and in zip.
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:
Finally here is the UDF itself:
And all 5 files in zip format: StringSize.zip
I hope you find this useful - I certainly do.
M23