Sai
Sai

Reputation: 461

Get size of SWT Label with word wrap

Is there a way to get the exact size computed by a label before laying it out.

In other words, let us say there is a label with a really long sentence:

Hi how are you doing, my name is bond, I have no name, I live in London, but yet the world is my Home. I am here to play and enjoy.

This is a single line. however if the window is not large enough, then the label wraps into two lines.

I want to find out the final height that it will take up, before laying it out (this is so that I can adjust the font size accordingly so that it displays in a single line).

However if I do label.computeSize(SWT.DEFAULT, SWT.DEFAULT) it only returns the size that it needs without wrapping.

If there is a way to do it that would be great.

Upvotes: 2

Views: 1730

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

Fist of all the Label must be created with the SWT.WRAP style. Otherwise it will never wrap its text.

Then you need to tell computeSize what width the label should have. Otherwise computeSize doesn't know where to wrap and computes the size for the unwrapped text.

If you specify the desired width of the label, computeSize will return the neccesary height.

For example

Label label = new Label( shell, SWT.WRAP );
label.setText( "Snippets are minimal stand-alone programs that demonstrate specific techniques or functionality." );
Point size = label.computeSize( 200, SWT.DEFAULT );
System.out.println( size );  // Point {202, 47}

The returned size is a Point whose x field is (almost) the desired width and the y field denotes the necessary height to display the wrapped text.

On my system, the size is { 202, 47 } (the text spans two lines) but this may vary depending on the platform and font size.

Upvotes: 1

Ardeshana Milan
Ardeshana Milan

Reputation: 373

Agree with Rudiger Herrmann's answer. I have done it previously for my purpose. But it was dependent on OS. I have adjusted size of the font to make label in a line in a Grid. Please find comments in code.

parent.setLayout(new GridLayout(1, false));
label = new Label( parent, SWT.BORDER | SWT.WRAP );
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
label.setTouchEnabled(true);
label.setToolTipText("LABEL");
label.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
label.setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
label.setText( "Stack Overflow is a question and answer site for professional and enthusiast programmermers. It's built and run by you as part of the Stack Exchange network of Q&A sites." );
Point parentCompositeSize = parent.getShell().getSize();
System.out.println(parentCompositeSize);
// parentCompositeSize.x is width of parent window (Shell) width, if you have your desired width of (window / Composite) then you can give it direct in int
// Calculation of number of lines with default font ::
// Warning:: Calculations have some assumption as below.
//1. Spacing between two lines will be 1 unit.
//2. My Layout used is GRID Layout.
Point estimatedLabelSize = label.computeSize( parentCompositeSize.x, SWT.DEFAULT );
System.out.println( "Estimated Label Height " + estimatedLabelSize.y);  
FontData[] fD = label.getFont().getFontData();
int defaultFontSize = fD[0].getHeight(); //Font Height is 11 in my case.
defaultFontSize += 1; //1 is spacing between two lines, According to assumption
System.out.println(defaultFontSize + "default size");
int lines = estimatedLabelSize.y / defaultFontSize;
System.out.println("Default fonts will print " + lines + " lines" );
int suggestedFontSize = defaultFontSize / lines;
System.out.println(suggestedFontSize +" suggested size ");

fD[0].setHeight(suggestedFontSize);
final Font newFont = new Font(parent.getDisplay(), fD);
label.setFont(newFont);
label.addDisposeListener(new DisposeListener() {

    @Override
    public void widgetDisposed(DisposeEvent e) {
        newFont.dispose();
    }
});

Upvotes: 1

Related Questions