Blue
Blue

Reputation: 1939

GWT - split string according to the chars physical length (not number of chars)

I have a gwt button that has a fixed size. I want to present a text on it, and if that text exceeds the button length, I need to cut it short and put "..." in the end. I use the following code now:

if(name.length() > 11){
    //limiting the size of the text presented on the button
    name = name.substring(0,10)+"...";
}

I have a list of such buttons, and this approach causes that not all button's text have the same size.

The problem is, for example, that 10 chars of "!" are much shorter than 10 chars of "A".

Can anyone help me with a way to solve this issue?

Thanks!

Upvotes: 1

Views: 183

Answers (2)

Bar Mako
Bar Mako

Reputation: 133

You could add a style-class to all those buttons and fix their size. Should be something like this:

First, add the style-class to each button:

    Button button = new Button();
    button.addStyleName("buttonSizeStandard");

Then add a matching style in the css file:

    .buttonSizeStandard{ 
         width: 200px;      //change 200 to whatever 
    }

Upvotes: 0

helios
helios

Reputation: 13841

Fast answer. Try ussing HTML5/CSS3 property. It does exactly that:

http://www.w3schools.com/cssref/css3_pr_text-overflow.asp

If you need to do that for non-CSS3 browsers you'll need to make some kind of fake. Maybe setting a button div content, with overflow-hidden, and placing a floating "..." to the right. But it's more difficult...

Upvotes: 3

Related Questions