ByteRipper Posted July 3, 2022 Share Posted July 3, 2022 (edited) I am trying to fit a string to a label. The string gets read from a ini. How can i calculate the font size in dependency of the width of the label, so when ever the user changes the string the maximum possible string size is used? I can't use StringSize.au3 because there are other labels and i can't change the hight of the label. #include <GUIConstants.au3> $sTextA = "Appname Appname" $sTextB = "Appname Appname Appname" $hGUI = GUICreate("Test", 620, 400) $cLabelA = GUICtrlCreateLabel($sTextA, 6, 10, 600, 75) GUICtrlSetFont(-1, 42, 800, 0, "Arial Black", 5) GUICtrlSetBkColor(-1, 0xCCFFCC) $cLabelB = GUICtrlCreateLabel($sTextB, 6, 90, 600, 75) GUICtrlSetFont(-1, 42, 800, 0, "Arial Black", 5) GUICtrlSetBkColor(-1, 0xCCFFCC) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Edited July 3, 2022 by ByteRipper Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 3, 2022 Moderators Share Posted July 3, 2022 ByteRipper, You can use StringSize - just iterate through the font sizes until you find the larges one that fits. Here is an example of what I mean: I hope that helps. M23 ByteRipper 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ByteRipper Posted July 3, 2022 Author Share Posted July 3, 2022 (edited) Thank's for your fast answer. The first label works if i take the second one out. On the second one i get a error "(40) : ==> Subscript used on non-accessible variable.:" for $aRet[3]. If i change the text on $sTextA to "Appname" for exaple i get the same error. expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <FontConstants.au3> #include <Array.au3> #include <StringSize.au3> $sTextA = "A. Font Size = " $sTextB = "Appname Appname Appname" $hGUI = GUICreate("Test", 620, 400) $iFontSize = _FindMaxSize($sTextA, 600, 75, 600, 0, "Arial Black") $cLabelA = GUICtrlCreateLabel($sTextA, 6, 10, 600, 75) GUICtrlSetFont(-1, $iFontSize, 600, 0, "Arial Black", 5) GUICtrlSetBkColor(-1, 0xCCFFCC) ;~ $iFontSize = _FindMaxSize($sTextB, 600, 75, 600, 0, "Arial Black") ;~ $cLabelB = GUICtrlCreateLabel($sTextB, 6, 90, 600, 75) ;~ GUICtrlSetFont(-1, $iFontSize, 600, 0, "Arial Black", 5) ;~ GUICtrlSetBkColor(-1, 0xCCFFCC) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _FindMaxSize($sTxt, $iWidth, $iHeight, $iWeight, $iAttribute, $sFontName) ; Set an initial font size $iFontSize = 120 While 1 ; Size the label needed to fit the string into a label of the correct width $aRet = _StringSize($sTxt, $iFontSize, $iWeight, $iAttribute, $sFontName, $iWidth) ; Now check if the height will fit If $aRet[3] < $iHeight Then ; If it does return the font size ExitLoop Else ; If not then reduce the font size and try again $iFontSize -= 1 EndIf WEnd Return $iFontSize EndFunc Edited July 3, 2022 by ByteRipper Link to comment Share on other sites More sharing options...
pixelsearch Posted July 3, 2022 Share Posted July 3, 2022 @Melba23 maybe this can help : If I run your script (the one you indicated in your post) with these changes : * 1 line added before GUICreate : Local $iGUI_Width = 600 * Replace all 1072 width constants in the script with $iGUI_Width * 1 line added to check @error : $aRet = _StringSize($sTxt, $iFontSize, $iWeight, $iAttribute, $sFontName, $iWidth) ConsoleWrite("@error = " & @error & @crlf) If $aRet[3] < $iHeight Then Now we run the amended script, @error will be = 0 plenty of times, then @error = 3 and the script ends abruptly, console showing : ...... @error = 0 @error = 0 @error = 0 @error = 3 "test.au3" (67) : ==> Subscript used on non-accessible variable.: If $aRet[3] < $iHeight Then If $aRet^ ERROR >Exit code: 1 For what it's worth Link to comment Share on other sites More sharing options...
Moderators Solution Melba23 Posted July 3, 2022 Moderators Solution Share Posted July 3, 2022 ByteRipper, Some simple error-checking gives the answer to your script problem - a pity no-one ever seems to bother to do this as all my UDFs have comprehensive error returns to explain why they might have failed. In this case, calling the _FindMaxSize function on the second text with a large font size produces an error from StringSize: Quote 3 - Font too large for chosen max width - a word will not fit So you need to add a check to see if this is the case: Func _FindMaxSize($sTxt, $iWidth, $iHeight, $iWeight, $iAttribute, $sFontName) ; Set an initial font size $iFontSize = 120 While 1 ; Size the label needed to fit the string into a label of the correct width $aRet = _StringSize($sTxt, $iFontSize, $iWeight, $iAttribute, $sFontName, $iWidth) If @error = 3 Then $iFontSize -= 1 ContinueLoop EndIf ; Now check if the height will fit If $aRet[3] < $iHeight Then ; If it does return the font size ExitLoop Else ; If not then reduce the font size and try again $iFontSize -= 1 EndIf WEnd Return $iFontSize EndFunc With that addition, your script runs perfectly for me. M23 ByteRipper 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
ByteRipper Posted July 3, 2022 Author Share Posted July 3, 2022 (edited) Thanks again for your respond and help!!! With the change it works until the String is > 36 long, it starts a second line, but that's good enough for me. Hope the users of my Programm are not using Appnames longer than that. Edited July 3, 2022 by ByteRipper Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now