获取带自动换行的 SWT 标签的大小
Get size of SWT Label with word wrap
有没有办法在布置标签之前获取标签计算出的准确尺寸。
换句话说,假设有一个带有很长句子的标签:
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.
这是单行。但是,如果 window 不够大,则标签会换成两行。
我想在布局之前找出它最终占用的高度(这样我就可以相应地调整字体大小,使其显示在一行中)。
但是,如果我这样做 label.computeSize(SWT.DEFAULT, SWT.DEFAULT)
它只会 returns 它需要的大小而不包装。
如果有办法做到这一点就好了。
所有 Label
的拳头必须使用 SWT.WRAP
样式创建。否则它永远不会换行。
然后你需要告诉 computeSize
标签应该有什么宽度。否则 computeSize
不知道在哪里换行并计算展开文本的大小。
如果您指定了所需的标签宽度,computeSize
将 return 指定所需的高度。
例如
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}
returned size
是一个 Point
,其 x
字段(几乎)是所需的宽度,y
字段表示必要的高度显示换行的文本。
在我的系统上,大小为 { 202, 47 }(文本跨越两行),但这可能因平台和字体大小而异。
同意 Rudiger Herrmann 的 。
为了我的目的,我之前已经这样做了。但它依赖于 OS。我已经调整了字体的大小以在网格中的一行中制作标签。请在代码中查找注释。
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();
}
});
有没有办法在布置标签之前获取标签计算出的准确尺寸。
换句话说,假设有一个带有很长句子的标签:
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.
这是单行。但是,如果 window 不够大,则标签会换成两行。
我想在布局之前找出它最终占用的高度(这样我就可以相应地调整字体大小,使其显示在一行中)。
但是,如果我这样做 label.computeSize(SWT.DEFAULT, SWT.DEFAULT)
它只会 returns 它需要的大小而不包装。
如果有办法做到这一点就好了。
所有 Label
的拳头必须使用 SWT.WRAP
样式创建。否则它永远不会换行。
然后你需要告诉 computeSize
标签应该有什么宽度。否则 computeSize
不知道在哪里换行并计算展开文本的大小。
如果您指定了所需的标签宽度,computeSize
将 return 指定所需的高度。
例如
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}
returned size
是一个 Point
,其 x
字段(几乎)是所需的宽度,y
字段表示必要的高度显示换行的文本。
在我的系统上,大小为 { 202, 47 }(文本跨越两行),但这可能因平台和字体大小而异。
同意 Rudiger Herrmann 的
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();
}
});