在 swt 中为多行文本设置要显示的行数
问题描述:
我正在为 TextArea 使用以下内容
I am using following for TextArea
ToolBar bar = new ToolBar(box,SWT.NONE);
ToolItem item = new ToolItem(bar, SWT.SEPARATOR);
Text text = new Text(bar, SWT.BORDER | SWT.MULTI);
item.setWidth(width);
item.setControl(text);
GridData data = new GridData();
data.verticalAlignment = SWT.CENTER;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
text.setLayoutData(data);
我想显示一个多行文本框,目前它接受多行文本但一次只显示一行.
I want to display a multi line text box, currently its accepting multi line text but showing only a single line at a time.
知道如何设置要显示的行数吗?
Any idea how to set the number of rows to be displayed ?
谢谢.
答
您可以以像素为单位设置高度:
You can set the height in pixels:
/* Set the height to 75 pixels */
data.heightHint = 75;
但是,您也可以根据字符行数设置高度,但是您必须做一些技巧来测量字符高度.您需要构建一个图形上下文 (GC
) 来测量文本范围.
However, you can also set the height in terms of the number of character rows, but you have to do some trickery to measure the character height. You'll need to build a graphics context (GC
) to measure the text extent.
例如:
GC gc = new GC(text);
try
{
gc.setFont(text.getFont());
FontMetrics fm = gc.getFontMetrics();
/* Set the height to 5 rows of characters */
data.heightHint = 5 * fm.getHeight();
}
finally
{
gc.dispose();
}