[Gambas-user] Label control

gian bagoneo at libero.it
Thu Jun 16 16:08:55 CEST 2022


Il 15/06/22 14:52, gian via User ha scritto:
> Il 15/06/22 00:26, Steve via User ha scritto:
>> I am attempting to populate a label control. The text on the control 
>> will vary. The size of the control does not. Is there a way to detect 
>> when the text Will overflow the control so that I can split the text 
>> up properly to display? This would be in the case where there is 
>> sufficient vertical room but not horizontal. Also the font size will 
>> vary.
> 
> The TextLabel has Autoresize and Wrap properties that, if i undestand, 
> you should need.
> Why can't you use them?
> 
> Regards
> Gianluigi
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----

OK, I didn't understand :-)

Then try this code in a new graphic project:

'-------------------------------------------------------------------
Public Sub Form_Open()

   Dim s As String = "Lorem ipsum dolor sit amet, consectetur 
adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua."
   Dim iRows, iHeight, iNew As Integer
   Dim iFontHeight As Integer
   Dim fProp As Float
   Dim hFont As Font
   Dim hTextLabel As TextLabel

   With hTextLabel = New TextLabel(Me) As "TextLabel1"
     .X = 50
     .Y = 50
     .W = 100
     .H = 100
     .AutoResize = False
     .Wrap = True
     .Text = s
     .Border = 1
     hFont = .Font
   End With

   iRows = Rows(hTextLabel.Text, hTextLabel.Width, hFont)

   While (iRows * hFont.Height) > hTextLabel.Height
     hFont.Size -= 0.5
     iRows = Rows(hTextLabel.Text, hTextLabel.Width, hFont)
   Wend

End

Private Function Rows(value As String, iWidth As Integer, hFont As Font) 
As Integer

   Dim i As Integer
   Dim ss As String[]
   Dim sRow As String

   If hFont.TextWidth(value) < iWidth Then Return 1
   ss = Split(value, " ")
   For Each s As String In ss
     sRow &= " " & s
     If hFont.TextWidth(sRow) > iWidth Then
       sRow = s
       Inc i
     Endif
   Next
   Return i + 1

End
'--------------------------------------------------

Regards
Gianluigi




More information about the User mailing list