B4J Question How to tell if a Label Text has overflowed

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I have a label and sometime when I set the contents it overflows and I get the "..." at the end.
Thats OK

What I would like to do is check and see if it has overflowed. Is there a way to tell?
I tried looking at the string but it does not have the "..."

Thanks!
 
Solution
I know, I know, I was going back to bed but its always... Just 1 more compile, just 1 more...

It works!:
Dim jo1 As JavaObject = lblLocation.As(Label)
jo1.RunMethod("setEllipsisString", Array("/003"))
Log("IsOverFlowed: " & (jo1.RunMethodjo("lookup", Array As Object(".text")).RunMethodjo("getText",Null).As(String).EndsWith(" /003")))

Cableguy

Expert
Licensed User
Longtime User
I think that it is a combination of multiline as false and wraptext as true... the best way I can think of, is to mesure the text length and compare it to the labels length, minus pudding...
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I have a label and sometime when I set the contents it overflows and I get the "..." at the end.
Thats OK

What I would like to do is check and see if it has overflowed. Is there a way to tell?
I tried looking at the string but it does not have the "..."

Thanks!
As Cableguy said, you can measure the text width and compare it with the width of the label, here is the reference

 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I am surprised no one has written this yet, We have a ResizeText in B4A but not for B4J.
Looks like though you guys have given me all the info I need to write one.

Sounds like a Sunday morning project... :)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I use this kind of code:

B4X:
    Private pnl As B4XView                'the dummy panel
    pnl = xui.CreatePanel("")
    Private cvsDummy As B4XCanvas
    cvsDummy.Initialize(pnl)            'the dumm B4XCanvas
    Private txt As String
    txt = "123456789012345678901234"
    Private rect As B4XRect
    Private Padding As Int = 8dip
    rect = cvsDummy.MeasureText(txt, Label1.As(B4XView).Font)    'measurement of the text
    If rect.Width + Padding > Label1.Width Then
        Label1.As(B4XView).Width = rect.Width + Padding
    End If
    Label1.Text = txt

It uses a dummy B4XCanvas which needs a B4XView Panel to calculate the text width.
 
Upvote 0

zed

Well-Known Member
Licensed User
Fit the label to the text
B4J:
Dim Height, Width As Int
Dim obj As Reflector
lblTXT.Width = -2
lblTXT.Height = -2
obj.Target = lblTXT
Width = obj.RunMethod("getWidth")
Height = obj.RunMethod("getHeight")
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
There are a few suggestions here: https://stackoverflow.com/questions/17621862/test-for-label-overrun which may also help.

There is some good ideas here. Looking at this code.

Java:
import javafx.scene.control.Label;
import javafx.application.Platform;
import javafx.scene.text.Text;
...
Label label = new Label();
...
label.setEllipsisString("\003");
...
final String newText = "fef foo";
Platform.runLater(() -> {
    label.setText(newText);
    String actual = ((Text)label.lookup(".text")).getText();
    // \003 is octal for the aforementioned "end of text" control char
    if (actual.length() > 0 && actual.charAt(actual.length()-1) == '\003') {
        // Handle text now that you know it's clipped
    }
});

So I started this:
B4X:
' lblLocation is a small label on my form
lblLocation.Text = "BIG string - make me fit, OK? - do I overflow? please!"
' add the new EllipsisString
Dim jo As JavaObject = lblLocation.As(Label)
jo.RunMethod("setEllipsisString", Array("\003"))

I think this line is what I need: How do I do this in B4X? As you can see my JavaObject skills are baby like. LOL

Java:
String actual = ((Text)label.lookup(".text")).getText();

So this method should return text that includes the control charactor. Then I can check and see if it there.
I believe this should work if I can figure out the JAVA call above.
Thoughts?
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I found it.
1704601937956.png


So how does one cast to a java LabeledText object?
Only took 3 hours! LOL, but boy did I learn more then I wanted to know! LOL

Test code:
    Try
        Dim jo As JavaObject = lblLocation.As(Label)
        jo.RunMethod("setEllipsisString", Array("/003"))
        lblLocation.Text = "string - make me fit, OK? - do I overflow? please! -- BIG string - make me fit, OK? - do I overflow? please!  -- BIG string - make me fit, OK? - do I overflow? please!"
        lblLocation.TextSize = 30
        Sleep(100)
        Dim jo1 As JavaObject = lblLocation.As(Label)
        Log(jo1.RunMethodjo("lookup", Array As Object(".text")))
        Dim jl As JavaObject = (jo1.RunMethodjo("lookup", Array As Object(".text")))
        Log(jl.As(String))
        'Log(jl.RunMethodjo("getText",Null))
                
        'Log(out.As(String).Contains("\003"))
        'Log(out.As(String).Contains(Chr(3)))
        
    Catch
        
        Log(LastException)
    End Try
 
Last edited:
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I know, I know, I was going back to bed but its always... Just 1 more compile, just 1 more...

It works!:
Dim jo1 As JavaObject = lblLocation.As(Label)
jo1.RunMethod("setEllipsisString", Array("/003"))
Log("IsOverFlowed: " & (jo1.RunMethodjo("lookup", Array As Object(".text")).RunMethodjo("getText",Null).As(String).EndsWith(" /003")))
 
Last edited:
Upvote 0
Solution
Top