Text expansion

Post

Posted
Rating:
#1 (In Topic #1406)
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
I use a LCDLabel and make the text "W". If I now resize the LCDLabel the text resizes at the same time. Is there a way I can do this with a Label?

This code shows the issue: -

Code (gambas)

  1. Label1 As Label
  2. LCDLabel1 As LCDLabel
  3.  
  4. Public Sub Form_Open()
  5.  
  6.   With Me
  7.     .Text = "Resize me"
  8.     .Arrangement = Arrange.Horizontal
  9.     .H = 150
  10.     .W = 150  
  11.     .Padding = 5
  12.   End With
  13.  
  14.   With Label1 = New Label(Me)
  15.     .Alignment = Align.Center
  16.     .Expand = True
  17.     .Font.Size = 12
  18.     .Text = "W"
  19.  
  20.   With LCDLabel1 = New LCDLabel(Me)
  21.     .Expand = True
  22.     .Text = "W"
  23.  
  24.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Banned
Not by default, it's all about Font size for labels.

must be some workarounds though

this works on your above example (and if using different text/words)…

Code (gambas)

  1. Public Sub Form_Resize()
  2.  
  3.   ' if label text height and width are less than the label size then increase Font.Size
  4.   If Label1.Font.Height < Label1.Height And If Label1.Font.TextWidth(Label1.Text) < Label1.W Then
  5.     Do
  6.       If Label1.Font.Height < Label1.Height And If Label1.Font.TextWidth(Label1.Text) < Label1.W Then
  7.         Inc Label1.Font.Size
  8.       Else
  9.         Break
  10.       Endif
  11.     Loop
  12.  
  13.   ' if label text height or width is more than the label size then decrease Font.Size
  14.   Else If Label1.Font.Height > Label1.Height Or If Label1.Font.TextWidth(Label1.Text) > Label1.W Then
  15.     Do
  16.       If Label1.Font.Height > Label1.Height Or If Label1.Font.TextWidth(Label1.Text) > Label1.W Then
  17.         If Label1.Font.Size > 2 Then Dec Label1.Font.Size Else Break  ' dont set the font size too small or it errors
  18.       Else
  19.         Break
  20.       Endif
  21.     Loop
  22.  
  23.  
  24.  

you could also probably use a PictureBox and paint the text to a picture then have the PictureBox.Stretch enabled to make it fit.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
 Thanks Bruce, I like the PictureBox idea. I'll do some experimentation as there are 110 separate LCDlables or Labels.
Online now: No Back to the top

Post

Posted
Rating:
#4
Banned
Or something like this..
Then any label who's name is like "SLabel*" gets stretched.

Code (gambas)

  1. Public Sub Form_Resize()
  2.  
  3.   For Each o As Object In Me.Controls
  4.     If o.Name Like "SLabel*" Then ResizeLabel(o)
  5.   Next
  6.  
  7.  
  8. Public Sub ResizeLabel(Lbl As Label)
  9.  
  10.   With Lbl
  11.   ' if label text height or width is less than the label size then increase Font.Size
  12.   If .Font.Height < .Height And If .Font.TextWidth(.Text) < .W Then
  13.     Do
  14.       If .Font.Height + 1 < .Height And If .Font.TextWidth(.Text) + 1 < .W Then
  15.         Inc .Font.Size
  16.       Else
  17.         Break
  18.       Endif
  19.     Loop
  20.   ' if label text height or width is more than the label size then decrease Font.Size
  21.   Else If .Font.Height > .Height Or If .Font.TextWidth(.Text) > .W Then
  22.     Do
  23.       If .Font.Height > .Height Or If .Font.TextWidth(.Text) > .W Then
  24.         If .Font.Size > 2 Then Dec .Font.Size Else Break
  25.       Else
  26.         Break
  27.       Endif
  28.     Loop
  29.  
  30.  
  31.  

Could be easier than converting all into pictureboxes
If naming is like Label1 , Label2, etc you just add S to the start of the Label names you want to stretch.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
This is the progress so far, still using LCDLabels, just what you need to fill a cold winter's afternoon! Welcome to the WordClock. I based it on a clock I saw in a friend's house. Have a look at the price here?!!

Code (gambas)

  1. Private LCDLabel1 As LCDLabel
  2. Private Spring1 As Spring
  3. Private sLetters As String[] = ["ITLISASAMPM", "ACQUARTERDC", "TWENTYFIVEX", "HALFSTENFTO", "PASTERUNINE", "ONESIXTHREE", "FOURFIVETWO", "EIGHTELEVEN", "SEVENTWELVE", "TENSEOCLOCK"]
  4. Private lLCDs As New LCDLabel[]
  5. Private MinPanels As New Panel[]
  6. Private byIt As Byte[] = [0, 0, 2]
  7. Private byIs As Byte[] = [0, 3, 2]
  8. Private byAM As Byte[] = [0, 7, 2]
  9. Private byPM As Byte[] = [0, 9, 2]
  10. Private byTo As Byte[] = [3, 9, 2]
  11. Private byPast As Byte[] = [4, 0, 4]
  12. Private byOClock As Byte[] = [9, 5, 6]
  13. Private byPoints As Byte[][] = [[9, 5, 6], [2, 6, 4], [3, 5, 3], [1, 2, 7], [2, 0, 6], [2, 0, 10], [3, 0, 4], [2, 0, 10], [2, 0, 6], [1, 2, 7], [3, 5, 3], [2, 6, 4]] 'Oclock, Five, Ten, Quarter,Twenty, Twentyfive, Half , Twentyfive, Twenty,Quarter, Ten, Five
  14. Private byHr As Byte[][] = [[0, 0, 0], [5, 0, 3], [6, 8, 3], [5, 6, 5], [6, 0, 4], [6, 4, 4], [5, 3, 3], [8, 0, 5], [7, 0, 5], [4, 7, 4], [9, 0, 3], [7, 5, 6], [8, 5, 6]]
  15.  
  16. Public Sub Form_Open()
  17.  
  18.   Try system.Ignoreoverflow = True
  19.   BuildForm
  20.  
  21.  
  22. Public Sub Timer1_Timer()
  23.  
  24.   Dim byHour As Byte = Val(Format(Time(Now), "hh"))
  25.   Dim byMin As Byte = Val(Format(Time(Now), "nn"))
  26.   Dim byRemainder As Byte = byMin Mod 5
  27.   Dim bPM As Boolean
  28.  
  29.   SetAllLCDsGray
  30.  
  31.   byMin = byMin - byRemainder
  32.  
  33.   ShowText(byIt)
  34.   ShowText(byIs)
  35.  
  36.   If byHour > 12 Then
  37.     byHour = byHour - 12
  38.     If byMin > 30 Then Inc byHour
  39.     bPM = True
  40.   End If
  41.  
  42.   ShowText(byHr[byHour])
  43.   If bPM = True Then
  44.     'ShowText(byPM)
  45.   Else
  46.     'ShowText(byAM)
  47.  
  48.   'Oclock, Five, Ten, Quarter,Twenty, Twentyfive, Half , Twentyfive, Twenty,Quarter, Ten, Five
  49.   If byMin / 5 <> 0 Then
  50.     If byMin > 30 Then
  51.       ShowText(byTo)
  52.     Else
  53.       ShowText(byPast)
  54.     End If
  55.   End If
  56.  
  57.   ShowText(byPoints[byMin / 5])
  58.  
  59.   For byMin = 0 To 3
  60.     MinPanels[byMin].Background = Color.Default
  61.   Next
  62.  
  63.   For byMin = 0 To byRemainder - 1
  64.     MinPanels[byMin].Background = Color.Blue
  65.   Next
  66.  
  67.   Me.Refresh
  68.   Wait
  69.  
  70.  
  71. Public Sub ShowText(byVal As Byte[])
  72.  
  73.   Dim byLoop, byCount As Byte
  74.  
  75.   For byLoop = 0 To lLCDs.Max
  76.     If lLCDs[byLoop].Name Begins Str(byVal[0]) And lLCDs[byLoop].Name Ends Str(byVal[1]) Then
  77.       For byCount = 0 To byVal[2] - 1
  78.         lLCDs[byLoop + byCount].Foreground = Color.Red 'Settings["Foreground", Color.Red]
  79.         lLCDs[byLoop + byCount].Background = Color.Yellow 'Settings["Background", Color.Yellow]
  80.         lLCDs[byLoop + byCount].HighlightColor = Color.Yellow 'Settings["Highlight", Color.Yellow]
  81.       Next
  82.       Break
  83.     Endif
  84.   Next
  85.  
  86.  
  87. Public Sub SetAllLCDsGray()
  88.  
  89.   Dim byLoop As Byte
  90.  
  91.   For byLoop = 0 To lLCDs.Max
  92.     lLCDs[byLoop].Foreground = Color.LightGray 'Settings["FaintForeground", Color.LightGray]
  93.     lLCDs[byLoop].Background = Color.Default 'Settings["FaintBackground", Color.Default]
  94.     lLCDs[byLoop].HighlightColor = Color.Default'Settings["FaintHighlight", Color.Default]
  95.   Next
  96.  
  97.  
  98. Public Sub BuildForm()
  99.  
  100.   Dim byRow, byCol As Byte
  101.  
  102.   With Me
  103.     .Arrangement = Arrange.Vertical
  104.     .Padding = 5
  105.     .Height = 512
  106.     .Width = 512
  107.     .Background = Color.cyan
  108.  
  109.   For byRow = 0 To 9
  110.     With HBox1 = New HBox(Me) As "HBoxes"
  111.       .H = 28
  112.       .Expand = True
  113.       .Padding = 5
  114.     End With
  115.    
  116.     For byCol = 0 To 10
  117.       With LCDLabel1 = New LCDLabel(HBox1) As "LCDs"
  118.         .Alignment = Align.Center
  119.         .Text = sLetters[byRow][byCol]
  120.         .Foreground = Color.LightGray
  121.         .Expand = True
  122.         .Sheared = True
  123.         .Name = Str(byRow) & "-" & Str(byCol)
  124.       End With
  125.       lLCDs.Add(LCDLabel1)
  126.     Next
  127.   Next
  128.  
  129.   With HBox2 = New HBox(Me)
  130.     .H = 24
  131.     .Padding = 3
  132.  
  133.   Spring1 = New Spring(HBox2)
  134.  
  135.   For byRow = 0 To 3
  136.     With Panel1 = New Panel(HBox2)
  137.       .Background = Color.Default
  138.       .W = HBox2.H - (HBox2.Padding * 2)  
  139.     End With
  140.     MinPanels.Add(Panel1)
  141.     Spring1 = New Spring(HBox2)
  142.   Next
  143.  
  144.   With Timer1 = New Timer As "Timer1"
  145.     .Delay = 1000 ' Not sure!
  146.     .Trigger
  147.     .Start
  148.  
  149.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.