'Class module
Sub Class_Globals
Private cvs As Canvas
Public mLbl As Label
Private su As StringUtils
Type AutoTextSizeLabelTag (Name As String, Instance As AutoTextSizeLabel)
End Sub
Public Sub Initialize (Target As Object, EventName As String)
End Sub
Public Sub DesignerCreateView(Base As Panel, lbl As Label, props As Map)
Dim bmp As Bitmap
bmp.InitializeMutable(1,1) 'ignore
cvs.Initialize2(bmp)
Dim parent As Panel = Base.Parent
Dim tag As AutoTextSizeLabelTag
tag.Initialize
tag.Name = Base.Tag
tag.Instance = Me
lbl.Tag = tag
parent.AddView(lbl, Base.Left, Base.Top, Base.Width, Base.Height)
Base.RemoveView
mLbl = lbl
'mLbl.color = Colors.Red
mLbl.Gravity=Bit.Or(Gravity.CENTER, Gravity.center )
'
mLbl.Padding = Array As Int(0, 0, 0, 0)
Dim jo As JavaObject = mLbl
jo.RunMethod("setIncludeFontPadding", Array(False))
setText(mLbl.Text)
End Sub
Public Sub setText(value As Object)
mLbl.Text = value
Dim multipleLines As Boolean = mLbl.Text.Contains(CRLF)
Dim size As Float
For size = 2 To 280
If CheckSize(size, multipleLines) Then Exit
Next
size = size - 0.5
If CheckSize(size, multipleLines) Then size = size - 0.5
mLbl.TextSize = size
End Sub
'returns true if the size is too large
Private Sub CheckSize(size As Float, MultipleLines As Boolean) As Boolean
mLbl.TextSize = size
If MultipleLines Then
Return su.MeasureMultilineTextHeight(mLbl, mLbl.Text) > mLbl.Height
Else
Return cvs.MeasureStringWidth(mLbl.Text, mLbl.Typeface, size) > mLbl.Width Or _
su.MeasureMultilineTextHeight(mLbl, mLbl.Text) > mLbl.Height
End If
End Sub
Public Sub getText As Object
Return mLbl.Text
End Sub
Public Sub setTextSize(value As Int)
mLbl.TextSize = value
End Sub
Public Sub setTextColor(value As Int)
mLbl.TextColor = value
End Sub
Public Sub getTextColor() As Int
Return mLbl.TextColor
End Sub
Public Sub getTextSize() As Int
Return mLbl.TextSize
End Sub
Public Sub getTypeFace() As Typeface
Return mLbl.Typeface
End Sub
'Public Sub setPanelTextColor(value As Int)
' p.color = value
'End Sub
Public Sub AsView As View
Return mLbl
End Sub
Public Sub setFont(value As Typeface)
mLbl.Typeface = value
End Sub
Public Sub setGravity(value As Int)
mLbl.Gravity = value
End Sub