Jump to content

Recommended Posts

Posted (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 by ByteRipper
  • Moderators
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted (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.

#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 by ByteRipper
Posted

@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 :bye:

  • Moderators
  • Solution
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted (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 by ByteRipper
  • ByteRipper changed the title to [SOLVED] Calculate Fontsize

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...