I'm having certain issues with StringUtils function MeasureMultilineTextHeight
Non-english language strings are cut off (example attached)
I tried adding some constant to returned value from function but it does not work when lines exceed.
Is this a specific language/font issue ?
Is there an other method to measure height required or any autosize label which adapts its height if width is fixed or width if height is fixed, to fit the text (on the lines of label.autosize in vb)?
After a lot of iterations I have arrived at a solution which I am going to share for others who might face the same challenge.
The value 1.16 has no technical or scientific basis. It is derived by trying a lot of iterations by varying text / width / font size and number of lines to check what value gives visually acceptable solution.
B4X:
'this line measures the height with little error for non-english fonts
lbl.Height = su.MeasureMultilineTextHeight(lbl, txt)
'this one corrects the error (almost always)
lbl.Height = lbl.Height * 1.16
1. Its for B4j and not b4a but I'm presuming that since the core for both is same it wont matter much
2. The cropping continues - to confirm that its related to either the font or how java treats non-english fonts
I am attaching some images to show it varied with actually how many lines were there before the last line. original text single line rectangle with just blank lines inserted before sample text (this looks perfect) when number of lines is increased to 10 (gap is way too much) finally single word -proverbial out of the box !!!
When I did this demonstration, it was to measure texts in Spanish or English.
But you can modify and test in your language.
Here is the logic of the text measurement, check it and adjust it to your language.
Note:
the routine measures the height and width of each row of the text.
B4X:
For Each t As String In inTexts
FontHeight = Max(FontHeight, cvs.MeasureText(t, xui.CreateDefaultFont(inFontSize)).Height) 'max. text row height
Next
For Each t As String In inTexts
width = Max(width, cvs.MeasureText(t, xui.CreateDefaultFont(inFontSize)).Width) + 1 'maximal with
height = height + FontHeight + 1
Next
After a lot of iterations I have arrived at a solution which I am going to share for others who might face the same challenge.
The value 1.16 has no technical or scientific basis. It is derived by trying a lot of iterations by varying text / width / font size and number of lines to check what value gives visually acceptable solution.
B4X:
'this line measures the height with little error for non-english fonts
lbl.Height = su.MeasureMultilineTextHeight(lbl, txt)
'this one corrects the error (almost always)
lbl.Height = lbl.Height * 1.16