Jump to content

Label text line break


Info
 Share

Recommended Posts

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
WEnd

I want the text to drop down one line when 80 pixels of text are reached.

Link to comment
Share on other sites

  • Moderators

Info,

As far as I know you will need to add the breaks yourself - but my StringSize UDF makes it very easy to do:

#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

EndFunc

You can find the UDF in my sig.

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

 

Link to comment
Share on other sites

  • Moderators

Info,

Quite possibly, I do tend to help when and where I can. Delighted I could do so again in this occasion.

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

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 years later...

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 by AimHigher
Error in 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...