B4A Library [custom view] AutoTextSizeLabel

Status
Not open for further replies.
When you set the text of this custom label the text size is automatically modified to the largest possible size so that all the text is visible.

SS-2013-06-30_11.49.54.png


To use this view in your project, you should take the AutoTextSizeLabel class from the attached example and add it to your project. You should then add a Custom View with the designer and set its type to this class.

The following libraries are required:
- JavaObject
- StringUtils
 

Attachments

  • AutoTextSizeLabel.zip
    8.5 KB · Views: 1,223
Last edited:

ykucuk

Well-Known Member
Licensed User
Add this routine to the class (AutoTextSizeLabel):

It doesnt work.

B4X:
Public Sub GetTextColor As Int
    Return mLbl.TextColor
End Sub
B4X:
'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
 

BarryW

Active Member
Licensed User
When you set the text of this custom label the text size is automatically modified to the largest possible size so that all the text is visible.

SS-2013-06-30_11.49.54.png


To use this view in your project, you should take the AutoTextSizeLabel class from the attached example and add it to your project. You should then add a Custom View with the designer and set its type to this class.

The following libraries are required:
- JavaObject
- StringUtils

How can i get this using Panel.getview(0)?
 

iCAB

Well-Known Member
Licensed User
Hi All

So far I have been doing all layouts by code, so this is my first time using the designer and Custom views.
I am just wondering why i am unable to capture the click event, even though the label is enabled in the designer.

B4X:
Private Sub AutoTextSizeLabel1_CLick
    Log("not trigerred by default ")
End Sub

For the above to work, I have to add the line below in DesignerCreateView
B4X:
lbl.Initialize("AutoTextSizeLabel1")

I am wondering if there is a better way perhaps using "Initialize"

Thanks
iCAB
 
Status
Not open for further replies.
Top