Public Sub SetText(value As String, mLbl As Label)
Dim HighValue As Float = 2
Dim LowValue As Float = 1
Dim CurrentValue As Float
mLbl.Text = value
Dim multipleLines As Boolean = mLbl.Text.Contains(CRLF)
'need to set actual start values so find Gross Start/Stop values
'first is linear Growth with some arbitrary 'large' value 'going with 30
'this can be fine tuned based on size of the display that you are aiming for/have available vs size of your CustomViews
Do While Not(CheckSize(HighValue, multipleLines,mLbl))
LowValue = HighValue
HighValue = HighValue + 30
Loop
CurrentValue = (HighValue + LowValue)/2
'
'initial sizes set, now for binary approach
'
'adjust this to taste. I like it at 1, and I think it looks very nice... can go a little larger for even faster times
'smaller for closer hit to actual optimum, but sacrificing a little speed
'
Dim ToleranceValue As Float = .5
'ToleranceValue = 1
Dim currentResult As Boolean
Do While (CurrentValue - LowValue) > ToleranceValue OR (HighValue - CurrentValue) > ToleranceValue
currentResult = CheckSize(CurrentValue, multipleLines,mLbl)
If currentResult Then 'this means currentvalue is too large
HighValue = CurrentValue
Else 'currentValue is too small
LowValue = CurrentValue
End If
CurrentValue = (HighValue + LowValue) / 2
Loop
Dim offset As Int = 0
mLbl.TextSize = ((CurrentValue - ToleranceValue) + offset)
'debugLog("size:"& mLbl.TextSize)
End Sub
'--- returns true if the size is too large
'--- called from setText
Private Sub CheckSize(size As Float, MultipleLines As Boolean, mLbl As Label) As Boolean
Dim cvs As Canvas , bmp As Bitmap
bmp.InitializeMutable(1%x,1%y)
cvs.Initialize2(bmp)
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