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: 2,197
Last edited:

leitor79

Active Member
Licensed User
Longtime User
Hi Erel, thank you for your answer.

that's I think I've done. I have to assign properties manually:

B4X:
   mLbl.Initialize("lbl")
   Base.AddView(mLbl,0,0,0,0)
   mLbl.Wrap = False
   mLbl.textcolor = lbl.TextColor
   mLbl.Background = lbl.Background
   mLbl.TextSize = lbl.TextSize
   mLbl.text = lbl.Text
   mLbl.Top = lbl.Top
   mLbl.Height = lbl.Height
   mLbl.Left = lbl.Left
   mLbl.Width = lbl.Width

(remember my mLbl is an EditText)

The EditText is added, however, it still doesn't autosize the text. I have to implement Sub lbl_TextChanged (Old As String, New As String) within the AutoTextSizeLabel class, with the code of setText, in order to work (note lbl_TextChanged is triggered by the mLbl EditText).

I don't find this workaround "clean", I feel there should be a better approach than my unexpertised-patched class...

Regards!
 

klaus

Expert
Licensed User
Longtime User
You cannot simply replace the Label by an EditText.
This line
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
must remain as it is, by design.

Attached you have a modified version.
It can be improved, I only want to show the difference.

You might have a look at chapter 12.4 Custom views and following in the B4A User's Guide.

EDIT: removed the class, a new version is in post#66
 
Last edited:

leitor79

Active Member
Licensed User
Longtime User
Hello again;

So, the correct use to add an AutoSizeTextLabel into a (for example) a customlistview would be to call DesignerCreateView with a label and the panel I will add into the customlistview instead of using Panel.Add(label...)?
 

klaus

Expert
Licensed User
Longtime User
So, the correct use to add an AutoSizeTextLabel into a (for example) a customlistview would be to call DesignerCreateView...
No.
If you want to add a CustomView by code you need a routine to add it.
Attached the modified project.
 

Attachments

  • AutoTextSizeEditText.zip
    9.5 KB · Views: 434

leitor79

Active Member
Licensed User
Longtime User
No words to exprese gratitude, Klaus. I've been fighting with this for a day.

Thank you very much!
 

ykucuk

Well-Known Member
Licensed User
Longtime User
Hello,
I tried set sext with CS builder but doesn't work.
Any help?

B4X:
Dim cs As CSBuilder
     Label1.Text = cs.Initialize.Color(Colors.White).Append("Hello ").Pop.BackgroundColor(Colors.Yellow).Color(Colors.Black).Append("World! :-)").PopAll
 

ykucuk

Well-Known Member
Licensed User
Longtime User
I've uploaded a new version that works with CSBuilder.
is there possibility
a- get AutoTextSizeLabel with FindViewByTag function.
b- get Textcolor and Pop.BackgroundColor (created by CSBuilder)

thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
get AutoTextSizeLabel with FindViewByTag function.
Good question which is relevant for all custom views. Start a new thread and I'll explain.

get Textcolor and Pop.BackgroundColor (created by CSBuilder)
Make mLbl a public variable and you will be able to get its TextColor. Getting the properties from the CSBuilder is more difficult.
 

ykucuk

Well-Known Member
Licensed User
Longtime User
Good question which is relevant for all custom views. Start a new thread and I'll explain.


Make mLbl a public variable and you will be able to get its TextColor. Getting the properties from the CSBuilder is more difficult.

Hello erel.
I can get text of autotextsizelabel but unfortunately cant get text color when i use csbuilder
B4X:
For Each v As View In CurrentPanel.GetAllViewsRecursive

If v.Tag Is AutoTextSizeLabelTag Then

LogColor("autosize bulduk",Colors.Red)

Dim atag As AutoTextSizeLabelTag = v.Tag

textinput.Text= atag.Instance.Text

textinput.TextColor=atag.Instance.TextColor

End If

Next
 

ykucuk

Well-Known Member
Licensed User
Longtime 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
 
Status
Not open for further replies.
Top