I was very tired of changing textsizes and padding for all views by code for each view. So I took Klaus' snippet for adjusting the "perfect" textsize to fit in a label and put a little automation to it.
It's a loop getting all views on a view (here it is a scrollview), check what type of view it is and then sets the "perfect" textsize plus padding.
I took 0,8 for 80% of "full fitting" (looks better).
This works fine for me (labels and edittext). For sure someone can do it better (code). Please feel free to extend/comment/discuss...
It's a loop getting all views on a view (here it is a scrollview), check what type of view it is and then sets the "perfect" textsize plus padding.
I took 0,8 for 80% of "full fitting" (looks better).
This works fine for me (labels and edittext). For sure someone can do it better (code). Please feel free to extend/comment/discuss...
B4X:
Dim v As View
Dim JO As JavaObject
For i =0 To sv.Panel.NumberOfViews-1
v=sv.Panel.getview(i)
JO=v
Log(v)
Select True
Case v Is Button
Log(">> Button")
Case v Is Spinner
Log(">> Spinner")
Case v Is EditText
Log(">> Edittext")
Dim et As EditText
et=v
SetEditTextSize(et,et.Text)
JO.RunMethod("setPadding",Array As Object(5,0,0,0))
Case v Is Label
Log(">> Label")
Dim la As Label
la=v
SetLabelTextSize(la,la.Text)
JO.RunMethod("setPadding",Array As Object(5,0,0,0))
End Select
Next
Sub SetLabelTextSize(ex As Label, txt As String)
Dim dt As Float
Dim limit = 0.5 As Float
Dim h As Int
ex.Text = txt
ex.TextSize = 72
dt = ex.TextSize
h = stu.MeasureMultilineTextHeight(ex, txt)
Do While dt > limit OR h > ex.Height
dt = dt / 2
h = stu.MeasureMultilineTextHeight(ex, txt)
If h > ex.Height Then
ex.TextSize = ex.TextSize - dt
Else
ex.TextSize = ex.TextSize + dt
End If
Loop
ex.TextSize=ex.textsize * 0.8
End Sub
Sub SetEditTextSize(ex As EditText, txt As String)
Dim dt As Float
Dim limit = 0.5 As Float
Dim h As Int
ex.Text = txt
ex.TextSize = 72
dt = ex.TextSize
h = stu.MeasureMultilineTextHeight(ex, txt)
Do While dt > limit OR h > ex.Height
dt = dt / 2
h = stu.MeasureMultilineTextHeight(ex, txt)
If h > ex.Height Then
ex.TextSize = ex.TextSize - dt
Else
ex.TextSize = ex.TextSize + dt
End If
Loop
ex.TextSize=ex.textsize *0.8
End Sub