Info Posted November 11, 2015 Posted November 11, 2015 In my script, I'm dealing with long text strings that don't contain spaces. The current settings for a label control is to do a line break when the text is long and it identifies a space character.My question is if it's possible to make it do a line break when the label text reaches its borders, regardless of a space char.Here's an example#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("", 615, 438, 192, 124) $Label1 = GUICtrlCreateLabel("longgggggggggggggggggggggGgGgGgggggggggggg", 160, 112, 80, 34) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEndI want the text to drop down one line when 80 pixels of text are reached.
Moderators Melba23 Posted November 11, 2015 Moderators Posted November 11, 2015 Info,As far as I know you will need to add the breaks yourself - but my StringSize UDF makes it very easy to do:expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StringSize.au3> $sText = "longgggggggggggggggggggggGgGgGgggggggggggg" ; Add the line breaks $sNewtext = _BreakText($sText, 80) ; Get the required label height to fit the broken text $aSize = _StringSize($sNewText) $iLabelHt = $aSize[3] $hGUI = GUICreate("Test", 500, 500) $cLabel = GUICtrlCreateLabel($sNewText, 10, 10, 80, $iLabelHt) GUICtrlSetBkColor($cLabel, 0xFFCCCC) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _BreakText($stext, $iWidth) ; The text with line breaks $sNewText = "" ; Length of text $iTextLen = StringLen($sText) ; Current start position $iStart = 1 ; Move through the text and insert line breaks For $iTextChar = 1 To $iTextLen ; Get the partial text for this line $sPartText = StringMid($sText, $iStart, $iTextChar - $iStart) ; Measure it $aSize = _StringSize($sPartText) ; Just to show what is happening ConsoleWrite($sPartText & " - " & $aSize[2] & @CRLF) ; If too wide If $aSize[2] > $iWidth Then ; Remove one character and add to the new text $sNewText &= StringTrimRight($sPartText, 1) & @CRLF ; Set the start position for the new line $iStart = $iTextChar - 1 ; Just to show where we are int eh process ConsoleWrite(@CRLF & "Full string" & @CRLF & $sNewText & @CRLF) EndIf Next ; End of the text so add teh partial line that remains If $sPartText Then $sNewText &= StringTrimRight($sPartText, 1) EndIf ; Return the broken text Return $sNewtext EndFuncYou can find the UDF in my sig.M23 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: Reveal hidden contents 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
Info Posted November 11, 2015 Author Posted November 11, 2015 Thanks Melba I actually remember you helping me a few years ago as well
Moderators Melba23 Posted November 11, 2015 Moderators Posted November 11, 2015 Info,Quite possibly, I do tend to help when and where I can. Delighted I could do so again in this occasion.M23 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: Reveal hidden contents 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
Skysnake Posted November 20, 2015 Posted November 20, 2015 Impressive. I just did it manually. Skysnake Why is the snake in the sky?
AimHigher Posted January 26, 2020 Posted January 26, 2020 (edited) UPDATE: I realized this doesn't solve it completely so please disregard or even better delete the post. Sorry to "wake up" this old thread but I am hoping it is the right thing to do. First off, Melba's _StringSize is awesome. Helps a lot when building GUIs dynamically e.g. by reading inputs from a file and the label controls should be added relative to each other. I was looking for exactly the solution that was presented above, how to properly fit long "words" without spaces e.g. a URL, into a label. I found one issue with the _BreakText function. It cuts off the last two chars of the last line of the new string. It is easier to see if the long word is set to "longgggggggggggggggggggggGgGgGgggggggggggg123". The solution (works for me at least) is to make two edits to _BreakText ;Change ;FROM: ; Get the partial text for this line $sPartText = StringMid($sText, $iStart, $iTextChar - $iStart) ;TO: ; Get the partial text for this line $sPartText = StringMid($sText, $iStart, $iTextChar - $iStart + 1) ;Change ;FROM: ; End of the text so add the partial line that remains If $sPartText Then $sNewText &= StringTrimRight($sPartText, 1) EndIf ;TO: ; End of the text so add the partial line that remains If $sPartText Then $sNewText &= $sPartText EndIf Melba, thank for all your contribution to this forum and you awesome UDFs. Edited January 26, 2020 by AimHigher Error in code
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