'Scale the text size of target view based on new width of view
'The text will be always one line and the aspect ratio will be preserved
'Text size will be increased as long as the height of text is NOT greater than the height of view
'Width : The width of a view before any changes in its dimensions
'TextSize : The text size of a view before any changes in its dimensions
'
'Example :
'Dim T8TextSize1 as T8TextSize
' T8TextSize1.Initialize
' T8TextSize1.SingleLineAutoScale(TextView , 100dip , 12)
Public Sub SingleLineAutoScale(TargetView As Object , Width As Int , TextSize As Int)
Dim lbl As Label = TargetView
If getLineCount(TargetView) > 1 Then
'should be set maximum text size
CallSubDelayed3(Me,"SingleLineFitText",TargetView,True)
Return
Else
'is one line so continue
WidthOfLineBefore = getTextWidth(TargetView , lbl.Typeface ,TextSize)
WidthRatioBefore = WidthOfLineBefore / Width
End If
'set maximum text size
lbl.TextSize = 72
Rate = lbl.TextSize
DoEvents
'measure width of text after and height of text
WidthOfLineAfter = getTextWidth(lbl , lbl.Typeface ,lbl.TextSize)
WidthRatioAfter = WidthOfLineAfter / lbl.Width
'height of text can not be larger than the height of view
HeightOfText = getTextHeight(lbl)
'measure line count
linecount = getLineCount(lbl)
'set text size in a while loop
Do While Rate > 0.5 Or linecount > 1 Or HeightOfText > lbl.Height Or WidthRatioAfter > WidthRatioBefore
Rate = Rate / 2
If WidthRatioAfter > WidthRatioBefore Or HeightOfText > lbl.Height Or linecount > 1 Then
lbl.TextSize = lbl.TextSize - Rate
Else
lbl.TextSize = lbl.TextSize + Rate
End If
DoEvents
'measure width and height and line count with new text size
WidthOfLineAfter = getTextWidth(lbl , lbl.Typeface ,lbl.TextSize)
WidthRatioAfter = WidthOfLineAfter / lbl.Width
HeightOfText = getTextHeight(lbl)
linecount = getLineCount(lbl)
Loop
End Sub