Info

Label text line break

5 posts in this topic

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.

Share this post


Link to post
Share on other sites



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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Thanks Melba :) I actually remember you helping me a few years ago as well :P 

Share this post


Link to post
Share on other sites

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._______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

 

Share this post


Link to post
Share on other sites

Impressive.  I just did it manually. :)

 


Skysnake

Why is the snake in the sky?

Share this post


Link to post
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