MeasureMultilineTextHeight doesn't work with line spacing?

susu

Well-Known Member
Licensed User
Longtime User
Hi all,

I used this code to change the space between text lines of Label.

B4X:
   Dim Obj1 As Reflector
   Obj1.Target = label1
   Obj1.RunMethod3("setLineSpacing", 1, "java.lang.float", 1.3, "java.lang.float")

and MeasureMultilineTextHeight always return wrong height (shorter). Anyone can help? Thank you.
 

susu

Well-Known Member
Licensed User
Longtime User
So there's no way to solve it?
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Susu,
Have you tried adjusting the MeasureMultilineTextHeight by the proportions that you have adjusted the line spacing by?
Might just be a simple solution?

Douglas (;
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not with this method. Here is the code:
B4X:
   public int MeasureMultilineTextHeight(TextView TextView, String Text) {
      StaticLayout sl = new StaticLayout(Text, TextView.getPaint(), 
            TextView.getLayoutParams().width - TextView.getPaddingLeft() - TextView.getPaddingRight(),
            Alignment.ALIGN_NORMAL, 1, 0 , true);
      return sl.getLineTop(sl.getLineCount());
   }
You can create a small library that allows you to set the mult and add values instead of 1 and 0.
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
You can create a small library that allows you to set the mult and add values instead of 1 and 0.

Are you kidding? If I know how to create a library, I didn't ask you that question :D

However, I solved it by my way:

B4X:
' MeasureLabel : Fake label to measure the height of text. Set it visible = False
' txt : String that contain the text.
' RLabel : This label you really want to display

            rowheight = su.MeasureMultilineTextHeight(MeasureLabel, txt) 
            Obj1.Target = RLabel 
            before = Obj1.RunMethod("getLineHeight")    'Get the height of line BEFORE you change it
            Obj1.Target = RLabel 
            Obj1.RunMethod3("setLineSpacing", 0, "java.lang.float", 1.5, "java.lang.float")  'Change the space between lines
            Obj1.Target = RLabel 
            after = Obj1.RunMethod("getLineHeight")    'Get the height of line AFTER  you change it
            finalheight = ((after * rowheight)/before) + 20dip   'Add 20dip to have some space below the text.

This way maybe not the best but at least it worked for me. :D
 
Upvote 0
Top