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:

leitor79

Active Member
Licensed 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
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
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
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: 166

leitor79

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

Thank you very much!
 

Syd Wright

Well-Known Member
Licensed User
On the basis of Erel's class it is also possible to auto-format existing labels, i.e. without the need to use a custom view. This is what I did:

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Private Label1 As Label
    Private Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Label1.Color=Colors.RGB(50,50,80)
    Label2.Color=Colors.RGB(50,50,80)

    Label1.Text = "This is an extremely long sentence, for the purpose of testing this auto resize function! Repeat: This is a very long sentence, intended to test this auto resize function!"
    Label2.Text = "This is an extremely long sentence, for the purpose of testing this auto resize function! Repeat: This is a very long sentence, intended to test this auto resize function!"
    Label1.TextSize = AUTO_SIZE1(Label1)
    Label2.TextSize = AUTO_SIZE2(Label2,24,12)
    Log("Textsize-1=" &Label1.TextSize)
    Log("Textsize-2=" &Label2.TextSize)
    WACHT1(2500)
    Label1.Text = "This is a very long sentence, intended to test this auto resize function!"
    Label2.Text = "This is a very long sentence, intended to test this auto resize function!"
    Label1.TextSize = AUTO_SIZE1(Label1)
    Label2.TextSize = AUTO_SIZE2(Label2,24,12)
    Log("Textsize-1=" &Label1.TextSize)
    Log("Textsize-2=" &Label2.TextSize)
    WACHT1(2500)
    Label1.Text = "This is a very long sentence"
    Label2.Text = "This is a very long sentence"
    Label1.TextSize = AUTO_SIZE1(Label1)
    Label2.TextSize = AUTO_SIZE2(Label2,24,12)
    Log("Textsize-1=" &Label1.TextSize)
    Log("Textsize-2=" &Label2.TextSize)
    WACHT1(2500)
    Label1.Text = "This is a sentence"
    Label2.Text = "This is a sentence"
    Label1.TextSize = AUTO_SIZE1(Label1)
    Label2.TextSize = AUTO_SIZE2(Label2,24,12)
    Log("Textsize-1=" &Label1.TextSize)
    Log("Textsize-2=" &Label2.TextSize)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub AUTO_SIZE1(Llabel1 As Label) As Float
    Dim su As StringUtils
    Dim LHeight1 As Float
    Llabel1.TextSize = 128
    LHeight1 = su.MeasureMultilineTextHeight(Llabel1,Llabel1.Text) ' + 4dip
    Do While LHeight1 > Llabel1.height
      LHeight1 = su.MeasureMultilineTextHeight(Llabel1,Llabel1.Text) ' + 4dip
      If LHeight1 < Llabel1.height Then Return Llabel1.TextSize
      Llabel1.TextSize = Llabel1.TextSize - 1
      If Llabel1.TextSize<8 then Return 8
    Loop
    Return 8      'Textsize should never be smaller than 8
End Sub

Sub AUTO_SIZE2(Llabel1 As Label, LDefault1 As Float, LMin1 As Float) As Float
    Dim su As StringUtils
    Dim LHeight1 As Float
    Llabel1.TextSize = LDefault1
    LHeight1 = su.MeasureMultilineTextHeight(Llabel1,Llabel1.Text) ' + 4dip
    If LHeight1 < Llabel1.height Then Return Llabel1.TextSize

    Llabel1.TextSize = 128
    LHeight1 = su.MeasureMultilineTextHeight(Llabel1,Llabel1.Text) ' + 4dip
    Do While LHeight1 > Llabel1.height
      LHeight1 = su.MeasureMultilineTextHeight(Llabel1,Llabel1.Text) ' + 4dip
      If LHeight1 < Llabel1.height Then Return Llabel1.TextSize
      If LHeight1 < LMin1 Then Return LMin1
      Llabel1.TextSize = Llabel1.TextSize - 1
    Loop
    Return LMin1      'Textsize should never be smaller than 8
End Sub

Sub WACHT1(Duur1 As Long)
    Dim Wticks1 As Long
    Wticks1=DateTime.Now
    Do While DateTime.Now < Wticks1+Duur1
      DoEvents
    Loop
End Sub

With AUTO_SIZE2 it can be avoided that a short text becomes too large.
Similarly, a parameter has been added that avoids the textsize becoming too small.
 
Last edited:

Syd Wright

Well-Known Member
Licensed User
Thank you. I have used loops with DoEvents for years and never had any problems (unless these loops were used under Activity_create). I have read the thread(s) about Sleep(). It seems I will have to modify all my code to make it compatible with Android 7.
In VB6 I never liked the Sleep command because it froze everything. It seems in Android things are different.
PS: I can't find "Sleep" in B4A version 6.80 (which I am currently using).
 
Last edited:

ykucuk

Well-Known Member
Licensed 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
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

Administrator
Staff member
Licensed 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
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
 
Status
Not open for further replies.
Top