Jump to content

Font size adjuster


Recommended Posts

Got bored over Christmas so... any use to anyone?

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

; ============ Test Strings ==================
;$sText = "Please Visually Confirm Transfer..." & @CRLF & @CRLF & "A Total of   >>  " & StringFormat("%d", 99.999) & "  << " & "jpg" & " Files were Copied..."
;Local $sText = "C:\This\Is\A\Very\Long\Path\That\DOSE NOT Needs\To\Wrap\To\A\New\Line\Filename.txt"
Local $sText = "Mike is off to town"
;Local $sText = "Mike is off to town" & @crlf & "Goodbye" & @crlf & "Goodbye" & @crlf & "Goodbye" & @crlf & "Goodbye" & @crlf & "Goodbye" & @crlf & "Goodbye" & @crlf & "Goodbye"
;Local $sText = "Mike"
; =============  Test Fonts  =================
;Local $sFont = "Garamond"
Local $sFont = "Angelyta" ;... Should Default if you do not have this Font
;Local $sFont = "Arial"
;Local $sFont = "Comic Sans Ms"
; ============================================

Local $iAttribute = 2  ;>>>> Normal = 0 -- Italic = 2 -- Underlined = 4 -- Strike = 8
Local $iWeight = 800   ;>>>> THIN = 100 -- EXTRALIGHT = 200 -- LIGHT = 300 -- NORMAL = 400 -- MEDIUM = 500 -- SEMIBOLD = 600 -- BOLD = 700 --EXTRABOLD = 800 -- HEAVY = 900

Local $hGUI = GUICreate("Auto-adjust Font Size for Varied Text Paths", 400, 100)
Local $hLabel = GUICtrlCreateLabel($sText, 10, 10, 380, 80, BitOR(0x200, 0x1))
GUISetState(@SW_SHOW)


_AdaptFontSize($hLabel, $iWeight, $iAttribute, $sFont)
;_AdaptFontSize($hLabel, 100, 0, "")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _AdaptFontSize($hCtrl, $iWeight = 100, $iAttribute = 0, $sFont = "Arial")
    If Not IsString($sFont) Then $sFont = "Arial"
    Local $iFontSize = 5, $iXLength, $iYHeight, $hdListview
    Local $aPos = ControlGetPos("", "", $hCtrl)
    $sText = ControlGetText("", "", $hCtrl)
    If @error Then
        MsgBox(48, "Sorry, Unable to Adjust Font-Size...", "Incompatible Text Characteristics.", 5)
    Else
        ;ConsoleWrite($aPos[2] & " - " & $aPos[3] & " - " & $sText & @CRLF) ; Test-Point
        $hdListview = GUICtrlCreateListView("", 0, 0, 0, 0) ; dummy see below...
        _GUICtrlListView_AddItem($hdListview, $sText)
        Do
            $iFontSize += 0.1 ; Get max fit with small font incrementals
            GUICtrlSetFont($hdListview, $iFontSize, $iWeight, $iAttribute, $sFont)
            GUICtrlSetData($hCtrl, $sText) ; include this if you wish to FLASH-EMPHASIS update?
            $iXLength = _GUICtrlListView_GetStringWidth($hdListview, StringReplace($sText, @CRLF, " ")) ; Return String Length in Pixels
            $iYHeight = _GUICtrlListView_ApproximateViewHeight($hdListview) ; has a relational factor... * 3.8 see below...
            ;ConsoleWrite("String Length = " & $iXLength & @CRLF & @CRLF) ; Test-Point
        Until $iXLength > $aPos[2] Or $iYHeight > ($aPos[3] * 3.8) ; Strangely works slightly better then... Until $iXLength < $aPos[2] etc.
        $iFontSize = $iFontSize - 0.1 ; Step back into "Fit" Range... See Above
        GUICtrlSetFont($hCtrl, $iFontSize, $iWeight, $iAttribute, $sFont)
        GUICtrlSetData($hCtrl, StringReplace($sText, @CRLF, " "))
        ; ConsoleWrite("String Length = " & $iXLength & @CRLF & "String Height = " & $iYHeight & @CRLF & "Font Size = " & $iFontSize & @CRLF)    ; Test_Point
    endif
EndFunc   ;==>_AdaptFontSize

 

Edited by VocabMike
incorrect posting of code
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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